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!
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:The only change was adding the
Errors
decorator to the stack.Try this validator syntax.