14.11. Validators

A validator compares data to a set of requirements and returns a boolean value indicating its validity. Place validator class files in the validators folder under your module's folder.

Here is the skeleton of a validator that performs validation bar for the Foo module:

<?php
/**
 * Validator description
 *
 * @copyright   copyright info
 * @license     license info
 * @version     version info
 */
class Foo_Validator_Bar implements Zend_Validate_Abstract
{
    const NOTBAR = 'notbar';

    protected $_messageTemplates = array(
        self::NOTBAR = "The value is not bar"
    );

    /**
     * Validates values for bar-ness.
     *
     * @param   mixed       $value  The value to be validated.
     * @return  boolean     True if the value is bar, false otherwise.
     */
    public function isValid($value)
    {
        $this->_setValue($value);

        // if bar, return true
        if (isset($value) && is_string($value) && $value === 'bar') {
            return true;
        }

        // not bar, return false
        $this->_error(self::NOTBAR);
        return false;
    }
}

For details, see the Zend Framework documentation.

Perforce Chronicle - Release: 2012.2/486814