2

I have a Zend_Form_element:

$text=new Zend_Form_Element_Text('text');

I added a regex validator to it:

$text->addValidator('regex', false, array('/[\\?\\&]v=([^\\?\\&]+)/'));

How could I set a customized error message for the validator?

1 Answer 1

6

You can add custom validation error messages if you know what specific error codes the validator provides. In case of regex, I believe it gives an "regexNotMatch" error, so for this particular case, you could use:

$text->addValidator('regex', false, array(
    '/[\\?\\&]v=([^\\?\\&]+)/',
    'messages'=>array(
    'regexNotMatch'=>'There was some random custom error'
    )    
));

For more information, have a look here.

Some developers may wish to provide custom error messages for a validator. The $options argument of the Zend_Form_Element::addValidator() method allows you to do so by providing the key 'messages' and mapping it to an array of key/value pairs for setting the message templates. You will need to know the error codes of the various validation error types for the particular validator

Similar question here and here

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

Comments

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.