Unable to set custom validation messages in Zend_Form

I just can't figure out how to set custom validation messages in a Zend_Form object. Here is a sample 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 check the form for entering invalid data, the message "notDigits" will appear. I tried changing "notDigits" to Zend_Validate_Digits :: NOT_DIGITS, but it still does not work properly.

Any help is much appreciated!

+1
source share
3 answers

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

+2
source

Your syntax for customizing the custom message is correct. In the code example you provided, the only decorator for this 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 the following:

 $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.

+1
source

Try using syntax 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") )); 
0
source

Source: https://habr.com/ru/post/923916/


All Articles