phpzend-frameworkzend-validate

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!


Solution

  • 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());
            }
        }
    }