ZF-6354: Doubt about addError - Zend_Form

Description

Hello, I tried to find something about custom errors depending the value and validator of the form.... Can I set my own messages errors depending the validation of the form? for example: If I set a StringLength validator, I show the message: "You wrote less than 6 chars." On the same textfield, if the user write any number, show another message like "stupid! only chars!".

Comments

You don't need custom error messages!

Use another validator:


$form->addElement(
    'text',
    'foo',
    array(
        'label' => 'Foo',
        'validators' => array(
            array(
                'Digits',
                true,
                array(
                    'messages' => array(
                        Zend_Validate_Digits::NOT_DIGITS => 'stupid! only chars!',
                    )
                ),
            ),
            array(
                'StringLength',
                true,
                array(
                    'min' => 6,
                    'messages' => array(
                        Zend_Validate_StringLength::TOO_SHORT => 'You wrote less than 6 chars.',
                    ),
                )
            ),
        ),
    )
);

Or use regex:


$form->addElement(
    'text',
    'foo',
    array(
        'label' => 'Foo',
        'validators' => array(
            array(
                'Regex',
                true,
                array(
                    'pattern'  => '/^[0-9]+$/', 
                    'messages' => array(
                        Zend_Validate_Regex::NOT_MATCH => 'stupid! only chars!',
                    )
                ),
            ),
            array(
                'StringLength',
                true,
                array(
                    'min' => 6,
                    'messages' => array(
                        Zend_Validate_StringLength::TOO_SHORT => 'You wrote less than 6 chars.',
                    ),
                )
            ),
        ),
    )
);