Zend_Validate_Abstract custom validator not displaying correct error messages.

Posted by Jeremy Dowell on Stack Overflow See other posts from Stack Overflow or by Jeremy Dowell
Published on 2010-05-14T23:08:42Z Indexed on 2010/05/14 23:14 UTC
Read the original article Hit count: 311

Filed under:

I have two text fields in a form that I need to make sure neither have empty values nor contain the same string.

The custom validator that I wrote extends Zend_Validate_Abstract and works correctly in that it passes back the correct error messages. In this case either: isEmpty or isMatch.

However, the documentation says to use addErrorMessages to define the correct error messages to be displayed.

in this case, i have attached

->addErrorMessages(array("isEmpty"=>"foo", "isMatch"=>"bar"));

to the form field.

According to everything I've read, if I return "isEmpty" from isValid(), my error message should read "foo" and if i return "isMatch" then it should read "bar".

This is not the case I'm running into though. If I return false from is valid, no matter what i set $this->_error() to be, my error message displays "foo", or whatever I have at index[0] of the error messages array.

If I don't define errorMessages, then I just get the error code I passed back for the display and I get the proper one, depending on what I passed back.

How do I catch the error code and display the correct error message in my form?

The fix I have implemented, until I figure it out properly, is to pass back the full message as the errorcode from the custom validator. This will work in this instance, but the error message is specific to this page and doesn't really allow for re-use of code.

Things I have already tried: I have already tried validator chaining so that my custom validator only checks for matches:

->setRequired("true")  
->addValidator("NotEmpty")  
->addErrorMessage("URL May Not Be Empty")  
->addValidator([*customValidator]*)  
->addErrorMessage("X and Y urls may not be the same")  

But again, if either throws an error, the last error message to be set displays, regardless of what the error truly is.

I'm not entirely sure where to go from here.

Any suggestions?

© Stack Overflow or respective owner

Related posts about zend