Can't set custom validator messages in Zend_Form

為{幸葍}努か 提交于 2019-12-08 04:57:13

问题


I just can't figure it out how to set custom validator messages in Zend_Form object. Here is an example code.

$this->addElement('password', 'password', array(
        'label'      => 'Password',
        'decorators' => array('ViewHelper'),
        'filters'    => array('StringTrim'),
        'validators' => array(
            array('Digits', false, array('messages' => array('notDigits' => 'Only digits are allowed here')))
        ),
        'required'   => true

    ));

When I try to validate the form entering invalid data a message saying "notDigits" appear. I tried to change 'notDigits' to Zend_Validate_Digits::NOT_DIGITS, but it still doesn't work as expected.

Any help is greatly appreciated!


回答1:


I found my error. I was recieving the 'notDigits' message, because in the controller I used $form->getErrors() method instead of $form->getMessages(). The first one returns only the error codes, without the messages.




回答2:


Your syntax for setting the custom message is correct. In the code example you posted, the only decorator for that element is ViewHelper so the error message will not be displayed.

At the very least, add the Errors decorator if you want to see the error message. Try this:

$this->addElement('password', 'code', array(
    'label'      => 'Code',
    'decorators' => array('ViewHelper', 'Errors'),
    'filters'    => array('StringTrim'),
    'validators' => array(
        array('Digits', false,
            array('messages' => array('notDigits' => 'Only digits are allowed here')))
    ),
    'required'   => true
);

The only change was adding the Errors decorator to the stack.




回答3:


Try this validator syntax.

$this->addElement("text", "fullname", array(
                        'label' => 'Your Full Name: ',
                        'required' => 'true',
                        'validators' => array(
                            array('validator' => 'StringLength', 'options' => array('min'=>5, 'max'=>250, 'messages' => array('stringLengthTooShort' => 'The name is too short.'))) 
                        ),
                        'filters' => array('StringTrim'),
                        'decorators' => array("signup")
                    ));


来源:https://stackoverflow.com/questions/9517524/cant-set-custom-validator-messages-in-zend-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!