0

当我们在 zend 中为表单元素添加装饰器时,验证消息没有显示为什么?

代码示例:

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

2 回答 2

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在您设置的装饰器中拥有一个装饰器。

于 2012-08-29T12:33:06.337 回答
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'))
    );
    }
于 2012-08-29T13:50:53.183 回答