Can't set custom validator messages in Zend_Form

删除回忆录丶 提交于 2019-12-07 18:19:29

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.

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.

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