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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?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