Can't set custom validator messages in Zend_Form
Asked Answered
S

3

1

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!

Sharasharai answered 1/3, 2012 at 14:0 Comment(0)
S
2

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.

Sharasharai answered 5/3, 2012 at 9:13 Comment(0)
A
1

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.

Apical answered 1/3, 2012 at 18:28 Comment(1)
Thank you for the reply drew010. I am not using Errors decorator, because i am capturing the errors in the controller using $form->getErrors() method, so I could display them in a separate DIV. The errors are displaying correctly, but the translation does not apply.Sharasharai
M
1

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")
                    ));
Marriageable answered 16/11, 2013 at 10:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.