2011-05-31 78 views
0

Zend talk.I在我的web应用程序中构建一个自定义的Zend_form。问题是我无法显示错误(当我提交没有任何文本的表单时)。我错过了明显的东西吗?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'); 
} 
} 

感谢

卢卡

回答

2

正确装饰到您的窗体上使用是FormErrors,如

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

的错误装饰是元素。

2

你必须把一个 “错误” 装饰的表单元素。 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')); 
    ... 
} 

因为你没有提供一个“错误”装饰覆盖这种行为,元级的错误做没有出现。