0

What I want to do:

Adding custom ErrorMessages to my Zend_Form_Element_Text when certain validations fail. But here is my problem: Whatever I tried there where only all custom messages displayed or the default. Even the first StringLength validation displays only both cases.

Short example what I do:

$usernameElement = new Zend_Form_Element_Text('username', array('label' => 'Username'));
$usernameElement->setRequired(true);
$usernameElement->addValidator(
            new Zend_Validate_StringLength(array('min' => 3, 'max' => 32),true)
        );
$usernameElement->addErrorMessages(array(
            Zend_Validate_StringLength::TOO_SHORT => 'Username is too short',
            Zend_Validate_StringLength::TOO_LONG => 'Username is too long'));

I wasted a painfull amount of time on this and know it must be a really stupid mistake :(

1 Answer 1

2

You need to add the custom messages to the validator, not the element.

Something like:

$validator = new Zend_Validate_StringLength(array(
    'min' => 3, 
    'max' => 32,
    'messages' => array(
        Zend_Validate_StringLength::TOO_SHORT => 'Username is too short',
        Zend_Validate_StringLength::TOO_LONG => 'Username is too long',
    ),
));
$element->addValidator($validator, true);

There are aggregated short forms for this that can be used during element creation, adding an element to a form, etc. But the upshot is that typically, you override the validator messages on the validator, not on the element.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot, that really was my mistake. I now using setMessages() on the validator and it works like a charm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.