2012-08-29 52 views
0

当我们向zend中的表单元素添加装饰器时,验证消息不显示为什么?Zend_Form装饰器:无法查看错误消息(验证)

代码示例:

$this->addElement('Text', 'Last_Name',array(
     //'decorators' => $this->elementDecoratorsTr, 
     'label' => 'Last Name:', 
     'required' => false, 
     'filters' => array('StringTrim'), 
      'validators' => array(array('validator' => 'StringLength','validator' => 'Alpha')) 
     )); 

回答

1

这里是Zend_Form_Element源代码:

$decorators = $this->getDecorators(); 
if (empty($decorators)) { 
    $this->addDecorator('ViewHelper') 
     ->addDecorator('Errors') // notice Errors decorator 
     ->addDecorator('Description', array('tag' => 'p', 'class' => 'description')) 
     ->addDecorator('HtmlTag', array('tag' => 'dd', 
             'id' => $this->getName() . '-element')) 
     ->addDecorator('Label', array('tag' => 'dt')); 
} 

如果您设置自己的装饰,则默认设置会不会被加载。

为了看到验证消息,您需要在您设置的装饰器中包含一个Errors装饰器。

+0

嗨ainokna,是的......我没有错误在我自己的装饰器装饰....现在我已经添加了错误装饰及其工作...谢谢你... – Punee

0

这里是和榜样对于错误味精设置装饰:

我们在有元素:

$title = $this->createElement('text', 'title'); 
    $title->setRequired(true) 
     ->setLabel('Title:') 
     ->setDecorators(FormDecorators::$simpleElementDecorators) 
     ->setAttrib('maxlength', $validationConfig->form->title->maxlength) 
     ->addValidator('stringLength', false, array($validationConfig->form->title->minlength, 
       $validationConfig->form->title->maxlength, 
       'encoding' => 'UTF-8', 
       'messages' => array(
        Zend_Validate_StringLength::INVALID => 
        'Title must be between %min% and %max% characters', 
        Zend_Validate_StringLength::TOO_LONG => 
        'Title cannot contain more than %max% characters', 
        Zend_Validate_StringLength::TOO_SHORT => 
        'Title must contain more than %min% characters')));      
    $this->addElement($title); 

,这是类形式的装饰,你可以做很多他们的存在:

class FormDecorators { 
    public static $simpleElementDecorators = array(
     array('ViewHelper'), 
     array('Label', array('tag' => 'span', 'escape' => false, 'requiredPrefix' => '<span class="required">* </span>')), 
     array('Description', array('tag' => 'div', 'class' => 'desc-item')), 
     array('Errors', array('class' => 'errors')), 
     array('HtmlTag', array('tag' => 'div', 'class' => 'form-item')) 
    ); 
    }