1. Define your custom validators

Example:

class MyValidate_MyFloat extends Zend_Validate_Abstract
{
    const FLOAT = 'float';
 
    protected $_messageTemplates = array(
        self::FLOAT => "'%value%' is not a floating point value"
    );
 
    public function isValid($value)
    {
        $this->_setValue($value);
 
        if (!is_float($value)) {
            $this->_error();
            return false;
        }
 
        return true;
    }
}

and, put this code in application/plugins/MyValidate/MyFloat.php

2. Tell Zend where to find your custom validators

change your application/Bootstrap.php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{

  // other _initXXXs
  // .....

  protected function _initValidate()
  {

    // Zend default namespaces is 'Zend_Validate'
    // and ,your namespaces is 'MyValidate'
    Zend_Validate::addDefaultNamespaces(array('MyValidate'));
  }

}

and change your application.ini

;; so ,Zend_Loader will auto load your Validator class
includePaths.plugin  = APPLICATION_PATH "/plugins"

3. OK, you can use your custom validator now.

Example:

$validator = new MyValidate_MyFloat();

if ($validator->isValid(0.324)) {

    // it's "MyFloat"

} else {

    // false

}

// or Using the static is() method

if (Zend_Validate::is(0.324, 'MyFloat)) {
    
   // Yes, it's "MyFloat"

}
Reference: Programmer's Reference Guide : Zend_Validate : Writing Validators

Date: 2011-12-29 22:13:02 and last modified: 2012-01-14 23:04:52

Relate tags:

Hot tags: