DatsunBing
DatsunBing

Reputation: 9076

Zend_Validate's isValid() method, and the $_POST array

In a book on the Zend Framework I have come across a custom validator for unique email addresses. The validator extends Zend_Validate_Abstract and therefore implements the isValid() interface.

In the interface the method signature is isValid($value). In the concrete class, it's isValid($value, $context = null).

The author explains that the $context variable contains the $_POST array and he relies on values from the array in the method's implementation. When I try to reproduce the code, however, my $context array is null. Furthermore, I am unable to find any reference to $context in the Zend_Validation reference guide, or to passing in the $_POST values.

Has anyone else come across this?

BTW, the book is Zend Framework 1.8 Web Application Development by Keith Pope.

Thanks!

Upvotes: 0

Views: 931

Answers (2)

brady.vitrano
brady.vitrano

Reputation: 2256

The $context array is passed when used with Zend_Form_Element::isValid method

Here is the snippet from Zend_Form_Element::isValid that is passing the $_POST as $context which is normally received from Zend_Form::isValid

foreach ($value as $val) {
    if (!$validator->isValid($val, $context)) {
        $result = false;
        if ($this->_hasErrorMessages()) {
            $messages = $this->_getErrorMessages();
            $errors   = $messages;
        } else {
            $messages = array_merge($messages, $validator->getMessages());
            $errors   = array_merge($errors,   $validator->getErrors());
        }
    }
}

Upvotes: 2

timdev
timdev

Reputation: 62894

It looks like the documentation for Zend_Validate is incomplete.

This behavior is described in the manual for Zend_Form_Element (ctl-f "Validation Context")

Upvotes: 1

Related Questions