Zend形式:不会显示错误

问题描述

| Zend talk。我在Web应用程序中构建了一个自定义的Zend_form。问题是我无法显示错误(当我提交不带任何文本的表单时)。我缺少明显的东西吗?
class Commentform extends Zend_Form
{
 public function init()
 {  

  $this->setMethod(\'post\');
  $this->setAction(\'\');
  $text=new Zend_Form_Element_Textarea(\'text\');
  $text->setrequired(true)
  ->addFilter(\'StringTrim\')
  ->addFilter(\'StripTags\')
  ->setDescription(\'bla bla\');

 $submit=new Zend_Form_Element_Submit(\'commenta\');

 $this->addElements(array($text,$submit));
 $this->setElementDecorators(array(
 \'ViewHelper\',array(\'Description\',array(
       \'tag\'=>\'span\',\'class\'=>\'medium\',\'placement\'=>\'PREPEND\')),));

$this->setDecorators(array(
\'FormElements\',\'FormErrors\',\'Form\',array(\'tag\'=>\'h2\',\'placement\'=>\'prepend\')),array(\'HtmlTag\',array(\'tag\' => \'div\',\'class\'=>\'write_comment\')),));

$this->setDescription(\'zend zend\');
}
}
谢谢 路卡     

解决方法

        在表单上使用的正确装饰器是FormErrors,例如
$this->setDecorators(array(
\'FormElements\',\'FormErrors\',\'Form\',array(\'Description\',array(\'tag\'=>\'h2\',\'placement\'=>\'prepend\')),array(\'HtmlTag\',array(\'tag\' => \'div\',\'class\'=>\'write_comment\')),));
错误装饰器用于元素。     ,        您必须在表单元素中放置一个\“ Errors \”装饰器。 Zend_Form_Element默认情况下会加载此\“ Errors \”装饰器,如您在Zend_Form_Element源代码中所见:
public function loadDefaultDecorators()
{
    ...
    $this->addDecorator(\'ViewHelper\')
        ->addDecorator(\'Errors\')
        ->addDecorator(\'Description\',array(\'tag\' => \'p\',\'class\' => \'description\'))
        ->addDecorator(\'HtmlTag\',array(\'tag\' => \'dd\',\'id\'  => array(\'callback\' => $getId)))
        ->addDecorator(\'Label\',array(\'tag\' => \'dt\'));
    ...
}
因为要覆盖此行为而没有提供\“ Errors \”装饰器,所以不会显示元素级错误。