[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/validation/src/rule/ -> lmbAtleastOneFieldRequiredRule.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  
  11  /**
  12   * Checks that at least one field from a list has not null value
  13   * Example of usage:
  14   * <code>
  15   * lmb_require('limb/validation/src/rule/lmbAtleastOneFieldRequiredRule.class.php');
  16   * $validator->addRule(new lmbAtleastOneFieldRequiredRule(array('name', 'nickname', 'fullname')));
  17   * </code>
  18   * @package validation
  19   * @version $Id: lmbAtleastOneFieldRequiredRule.class.php 5945 2007-06-06 08:31:43Z pachanga $
  20   */
  21  class lmbAtleastOneFieldRequiredRule implements lmbValidationRule
  22  {
  23    /**

  24    * @var array List of fields

  25    */
  26    protected $field_names;
  27    /**

  28    * @var string Custom error message

  29    */
  30    protected $custom_error;
  31  
  32    function __construct($field_names, $custom_error = '')
  33    {
  34      $this->field_names = $field_names;
  35      $this->custom_error = $custom_error;
  36    }
  37  
  38    /**

  39    * @see lmbValidationRule :: validate()

  40    */
  41    function validate($datasource, $error_list)
  42    {
  43      if(!$this->_findAtleastOneField($datasource))
  44      {
  45        $error = $this->custom_error ? $this->custom_error : $this->_generateErrorMessage();
  46        $error_list->addError($error, $this->field_names, array());
  47      }
  48    }
  49  
  50    protected function _findAtleastOneField($datasource)
  51    {
  52      foreach($this->field_names as $field_name)
  53      {
  54        if($value = $datasource->get($field_name))
  55          return true;
  56      }
  57  
  58      return false;
  59    }
  60  
  61    protected function _generateErrorMessage()
  62    {
  63      for($i = 0; $i < count($this->field_names); $i++)
  64        $fields[] = '{' . $i . '}';
  65  
  66      return lmb_i18n('Atleast one field required among: {fields}',
  67                       array('{fields}' => implode(', ', $fields)),
  68                      'validation');
  69    }
  70  }
  71  ?>


Generated: Tue Oct 7 05:02:03 2008 Cross-referenced by PHPXref 0.7