Zend_Validate_Callback with no context

This class is super simple, so we’ll begin with the code.

How does it work?

During Zend_Form->isValid(), every validator on the form is passed an extra parameter called the “context”. The context contains all of the data submitted with the form. This lets you validate one form field based on the value of another, like a Password Confirm field that must match a Password field.

One of the Zend Framework standard validators is Zend_Validate_Callback, which lets you validate the field using any function. For example, a Username field could validate with the following function:

The $excludeId parameter is supplied when the user has an existing account, and omitted for new user registrations. For example:

(In this case, you might also use Zend_Validate_Db_NoRecordExists, but I prefer not to leak so much model information into the form validation.)

What’s the problem?

As it stands, the Register.php form above will trigger a Zend_Validate_Callback::INVALID_CALLBACK error (“An exception has been raised within the callback”). That’s because the form context will be appended as a parameter to the callback and interpreted as the $excludeId, fouling the database lookup.

Cue the custom validator. By overriding the Zend_Validate_Callback->isValid() function, we’ll drop the form context before invoking the callback function. The registration form would be updated as follows:

Now you can setup callback validators without worrying about the default parameters of the callback function.

Leave a Reply

Your email address will not be published.