Custom meaningful error message for Zend RegEx Validator
Asked Answered
D

2

7

I'm validating a text field in my form as follows:

 $name = new Zend_Form_Element_Text('name');

 $name->setLabel('First Name:')
      ->setRequired(true)
      ->addFilter(new Zend_Filter_StringTrim())
      ->addValidator('regex',true,array('/^[(a-zA-Z0-9)]+$/'))
      ->addErrorMessage('Please enter a valid first name');

What I'm trying to accomplish is - how can i display a meaningful error message? Eg: If first name is 'XYZ-', how can i display '- is not allowed in first name.'

Is there a way I can access what character the regex is failing for? Would you recommend something else altogether?

I thought about writing a custom validator but the regex is pretty simple, so I don't see the point. I couldn't find a decent documentation for the zend 'regex' validator anywhere.

If I don't override the default error message, I simple get something like : ';;;hhbhbhb' does not match against pattern '/^[(a-zA-Z0-9)]+$/' - which I obviously don't want to display to the user.

I'd appreciate your inputs.

Diarmid answered 15/3, 2010 at 21:47 Comment(0)
D
1

How about telling the user in layman's terms what your restrictions are? Like

Error: Only the letters A to Z and numbers are allowed.

(Which leads me to the question why first names can contain numbers...)

Darvon answered 16/3, 2010 at 7:13 Comment(3)
Hi, this was just an example...could be any field (not necessarily first name) but is an example of the problem im facing thats all..Diarmid
I believe he was asking how to set a message, not what message to set.Haplo
@Marcus Downing: I don't think so. She already knows how to set a message (see her sample code), and she wanted to find out how to generate a message that is "tailored" to the user's input. Which I advised against.Darvon
S
16

For your custom error message(s) in zend standart validators just pass the messages array to the validator while instantiating. It's an array, which keys are error types (see further), and values - error messages.

->addValidator('regex', true, 
                       array(
                           'pattern'=>'/^[(a-zA-Z0-9)]+$/', 
                           'messages'=>array(
                               'regexNotMatch'=>'Your own custom error message'
                           )
                       )
)

To see error keys for other error types of chosen validator you may refer to it's source code. For regex validator it's located at {Zend Framework Library}/Zend/Validate/Regex.php .

Good luck in validating :).

Swampland answered 23/10, 2010 at 12:7 Comment(0)
D
1

How about telling the user in layman's terms what your restrictions are? Like

Error: Only the letters A to Z and numbers are allowed.

(Which leads me to the question why first names can contain numbers...)

Darvon answered 16/3, 2010 at 7:13 Comment(3)
Hi, this was just an example...could be any field (not necessarily first name) but is an example of the problem im facing thats all..Diarmid
I believe he was asking how to set a message, not what message to set.Haplo
@Marcus Downing: I don't think so. She already knows how to set a message (see her sample code), and she wanted to find out how to generate a message that is "tailored" to the user's input. Which I advised against.Darvon

© 2022 - 2024 — McMap. All rights reserved.