[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/validation/src/ -> lmbValidator.class.php (source)

   1  <?php
   2  /*
   3   * Limb PHP Framework
   4   *
   5   * @link http://limb-project.com 
   6   * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
   7   * @license    LGPL http://www.gnu.org/copyleft/lesser.html 
   8   */
   9  lmb_require('limb/validation/src/lmbErrorList.class.php');
  10  lmb_require('limb/core/src/lmbHandle.class.php');
  11  
  12  /**
  13   * Holds the list of validation rules along with errors happened during validation.
  14   * Validates a datasource against added validation rules.
  15   * @package validation
  16   * @version $Id: lmbValidator.class.php 5945 2007-06-06 08:31:43Z pachanga $
  17   */
  18  class lmbValidator
  19  {
  20    /**

  21    * @see lmbValidationRule

  22    * @var array List of added validation rules

  23    */
  24    protected $rules = array();
  25  
  26    /**

  27    * @var lmbErrorList List of validation errors

  28    */
  29    protected $error_list;
  30  
  31    /**

  32    * Constructor

  33    * @param lmbErrorList

  34    */
  35    function __construct($error_list = null)
  36    {
  37      $this->error_list = $error_list;
  38    }
  39  
  40    /**

  41    * Returns list of errors.

  42    * Creates an empty lmbErrorList if error list is NULL

  43    * @return lmbErrorList

  44    */
  45    function getErrorList()
  46    {
  47      if(!$this->error_list)
  48        $this->error_list = new lmbErrorList();
  49  
  50      return $this->error_list;
  51    }
  52  
  53    /**

  54    * Sets new list of errors

  55    * @return void

  56    */
  57    function setErrorList($error_list)
  58    {
  59      return $this->error_list = $error_list;
  60    }
  61  
  62    /**

  63    * Adds a new rule

  64    * @return void

  65    */
  66    function addRule($rule)
  67    {
  68      $this->rules[] = $rule;
  69    }
  70  
  71    /**

  72    * Alias for adding lmbRequiredRule to validator

  73    * @return void

  74    */
  75    function addRequiredRule($field, $custom_error = '')
  76    {
  77      $this->addRule(new lmbHandle('limb/validation/src/rule/lmbRequiredRule',
  78                                   array($field, $custom_error)));
  79    }
  80  
  81    function addAtLeastOneRequiredRule($fields, $custom_error = '')
  82    {
  83      $this->addRule(new lmbHandle('limb/validation/src/rule/lmbAtleastOneFieldRequiredRule',
  84                                   array($fields, $custom_error)));
  85    }
  86  
  87    /**

  88    * Alias for adding lmbRequiredObjectRule to validator

  89    * @return void

  90    */
  91    function addRequiredObjectRule($field, $class = null, $custom_error = '')
  92    {
  93      $this->addRule(new lmbHandle('limb/validation/src/rule/lmbRequiredObjectRule',
  94                                   array($field, $class, $custom_error)));
  95    }
  96  
  97    /**

  98    * Alias for adding lmbSizeRangeRule to validator

  99    * @return void

 100    */
 101    function addSizeRangeRule($field, $min_or_max_length, $max_length = NULL, $custom_error = '')
 102    {
 103      $this->addRule(new lmbHandle('limb/validation/src/rule/lmbSizeRangeRule',
 104                                   array($field, $min_or_max_length, $max_length, $custom_error)));
 105    }
 106  
 107    /**

 108    * @return boolean TRUE if list of errors is empty

 109    */
 110    function isValid()
 111    {
 112      return $this->getErrorList()->isValid();
 113    }
 114  
 115    /**

 116    * Performs validation

 117    * Passes datasource and list of errors to every validation rule

 118    * @param lmbSetInterface Datasource to validate

 119    * @return boolean True if valid

 120    */
 121    function validate($datasource)
 122    {
 123      foreach($this->rules as $rule)
 124        $rule->validate($datasource, $this->getErrorList());
 125  
 126      return $this->isValid();
 127    }
 128  }
 129  
 130  ?>


Generated: Fri Aug 29 04:49:26 2008 Cross-referenced by PHPXref 0.7