Zend_Validate_Callback Success on False

What if you need to validate input using a callback function that will return False when the input is valid? Here’s a custom validator to do the job.

When would you use this?

If you wrote the callback function, you control the result. But if your callback function is built into PHP or a library, it may not be so easy. You could wrap the function with a version that reverses the result, or you can use My_Validate_Callback_False.

For example, I have a user registration form with a CAPTCHA. New usernames are checked in the database for uniqueness. But I don’t want to query the database unless the user has passed the CAPTCHA — this combats automated queries that probe for usernames. So the username validation chain includes a callback to Zend_Form_Element_Captcha->hasErrors().

Because hasErrors() will return FALSE when the input is valid, I need My_Validate_Callback_False to reverse the normal behavior.

Leave a Reply

Your email address will not be published.