[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/validation/src/rule/ -> lmbSingleFieldRule.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/rule/lmbValidationRule.interface.php');
  10  lmb_require('limb/i18n/common.inc.php');
  11  
  12  /**
  13   * A base class for rules responsbile for validating a single field should inherit this class.
  14   * @package validation
  15   * @version $Id: lmbSingleFieldRule.class.php 5945 2007-06-06 08:31:43Z pachanga $
  16   */
  17  abstract class lmbSingleFieldRule implements lmbValidationRule
  18  {
  19    /**

  20    * @var string Field name

  21    */
  22    protected $field_name;
  23    /**

  24    * @see validate()

  25    * @var lmbErrorList List of errors.

  26    */
  27    protected $error_list;
  28    /**

  29    * @var string Custom error message

  30    */
  31    protected $custom_error;
  32  
  33    /**

  34    * @param string Field name

  35    */
  36    function __construct($field_name, $custom_error = '')
  37    {
  38      $this->field_name = $field_name;
  39      $this->custom_error = $custom_error;
  40    }
  41  
  42    /**

  43    * @return string Field name

  44    */
  45    function getField()
  46    {
  47      return $this->field_name;
  48    }
  49  
  50    /**

  51    * Alias for adding single field error to error list

  52    * Fills field array with array('Field' => $this->field_name) that is ok for single field rules

  53    * If $custom_error attribute is set will use $custom_error regardless of $message

  54    * If $custom_error attribute is not set will apply lmb_i18n function to $message

  55    * @param string Error message

  56    * @param array Array of values

  57    * @see lmbErrorList :: addError()

  58    * @return void

  59    */
  60    function error($message, $values = array(), $i18n_params = array())
  61    {
  62      $error = $this->custom_error ? $this->custom_error : lmb_i18n($message, $i18n_params, 'validation');
  63      $this->error_list->addError($error, array('Field' => $this->field_name), $values);
  64    }
  65  
  66    /**

  67    * Validates field

  68    * Calls {@link check()} method if $datasource has such field with not empty value.

  69    * Child classes must implement check($value) method to perform real validation.

  70    * To check field for existance and having none empty value use {@link lmbRequiredRule}

  71    * Fills {@link $error_list}

  72    * @see lmbValidationRule :: validate

  73    */
  74    function validate($datasource, $error_list)
  75    {
  76      $this->error_list = $error_list;
  77      $value = $datasource->get($this->field_name);
  78      if(isset($value) && $value !== '')
  79        $this->check($value);
  80    }
  81  
  82    /**

  83    * Performs real validation

  84    * @param mixed Field value check

  85    * @return void

  86    */
  87    abstract function check($value);
  88  }
  89  ?>


Generated: Tue Oct 14 04:47:40 2008 Cross-referenced by PHPXref 0.7