Zend表单:错误don';不要出现


Zend form: errors don't show up

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('Description',array('tag'=>'h2','placement'=>'prepend')),
 array('HtmlTag', array('tag' => 'div','class'=>'write_comment')),
));
$this->setDescription('zend zend');
}
}

感谢

Luca

在表单上使用的正确装饰器是FormErrors,例如

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

Errors装饰器用于元素。

您必须在表单元素中放置一个"Errors"装饰器Zend_Form_Element默认加载此"错误"装饰器,如您在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'));
    ...
}

因为您在不提供"错误"装饰器的情况下重写此行为,所以不会显示元素级错误。