Zend RegEx Validator error message issue
Posted
by Mallika Iyer
on Stack Overflow
See other posts from Stack Overflow
or by Mallika Iyer
Published on 2010-03-15T21:47:10Z
Indexed on
2010/03/15
21:49 UTC
Read the original article
Hit count: 428
Hello, 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.
© Stack Overflow or respective owner