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?

luca
  • 36,606
  • 27
  • 86
  • 125

1 Answers1

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

Community
  • 1
  • 1
Niklas
  • 29,752
  • 5
  • 50
  • 71