可捕获致命错误:类Zend \ Form \ View \ Helper \ FormLabel [关闭]的对象

可捕获致命错误:类Zend \ Form \ View \ Helper \ FormLabel [关闭]的对象

问题描述:

Catchable fatal error: Object of class Zend\Form\View\Helper\FormLabel could not be converted to string in (...)\module\Application\view\application\produtos\form.phtml on line 2

form.phtml

<?php echo $this->form()->openTag($form);?>
<?php echo $this->formLabel($form->get('nome_produto'));?>
<?php echo $this->formElement($form->get('nome_produto'));?>
<?php echo $this->formErrors($form->get('nome_produto'));?>
<?php echo $this->form()->closeTag($form);?>

ProdutosController.php

public function novoAction()
{
    $form = new ProdutoForm();

    $view = new ViewModel(array(
            'form' => $form
    ));
    $view->setTemplate('application/produtos/form.phtml');
    return $view;
}

ProdutoForm.php

    <?php

namespace Application\Form;

use Zend\Form\Form;
use Zend\Form\Element\Text;

class ProdutoForm extends Form
{
public function __construct($name = null) {
    parent::__construct('produto');

    $nome = new Text('nome_produto');
    $nome->setLabel('Nome: ')
         ->setAttributes(array(
            'class' => 'input-mediun'
         ));
    $this->add($nome);

...

可捕获的致命错误:类Zend \ Form \ View \ Helper \ FormLabel 的对象可以 不能转换为 (...)\ module \ Application \ view \ application \ produtos \ form.phtml中的字符串 2 p> blockquote>

表单 .phtml p>

 &lt;?php echo $ this-&gt; form() - &gt; openTag($ form);?&gt; 
&lt;?php echo $ this-  &gt; formLabel($ form-&gt; get('nome_produto'));?&gt; 
&lt;?php echo $ this-&gt; formElement($ form-&gt; get('nome_produto'));?&gt; \  n&lt;?php echo $ this-&gt; formErrors($ form-&gt; get('nome_produto'));?&gt; 
&lt;?php echo $ this-&gt; form() - &gt; closeTag($ form)  ;?&gt; 
  code>  pre> 
 
 

ProdutosController.php p>

  public function novoAction()
 {
 $ form  = new ProdutoForm(); 
 
 $ view = new ViewModel(array(
'form'=&gt; $ form 
)); 
 $ view-&gt; setTemplate('application / produtos / form.phtml  '); 
返回$ view; 
} 
  code>  pre> 
 
 

ProdutoForm.php p>

 &lt;?php  
 
 命名空间Application \ Form; 
 
use Zend \ Form \ Form; 
use Zend \ Form \ Element \ Text; 
 
class ProdutoForm extends Form 
 {
public function __construct($ name = null){
 parent:  :__ construct('produto'); 
 
 $ nome = new Text('nome_produto'); 
 $ nome-&gt; setLabel('Nome:')
  - &gt; setAttributes(array(
'class)  '=&gt;  'input-mediun'
)); 
 $ this-&gt; add($ nome); 
  code>  pre> 
 
 

... p>

Yeah those form view helpers should really have better error detection. I'm guessing $form->get('nome_produto') is returning null. As a result the FormLabel view helper is simply returning itself since it has nothing to actually render. The echo command is then trying to cast the view helper to a string.

Double check the element name and make sure you're getting the right item from the form.