[ Index ]

PHP Cross Reference of Web Application Component Toolkit

title

Body

[close]

/framework/validation/ -> rule.inc.php (source)

   1  <?php
   2  //--------------------------------------------------------------------------------
   3  // Copyright 2003 Procata, Inc.
   4  // Released under the LGPL license (http://www.gnu.org/copyleft/lesser.html)
   5  //--------------------------------------------------------------------------------
   6  /**
   7  * @package WACT_VALIDATION
   8  * @version $Id: rule.inc.php,v 1.1 2004/11/16 01:55:37 jeffmoore Exp $
   9  */
  10  
  11  /**
  12  * Base class for defining Rules to validate against
  13  * @see http://wact.sourceforge.net/index.php/Rule
  14  * @access public
  15  * @package WACT_VALIDATION
  16  * @abstract
  17  */
  18  class Rule {
  19      /**
  20      * Identifies error message group in vars file
  21      * @var string (default='validation')
  22      * @access private
  23      */
  24      var $Group = 'validation';
  25      
  26      /**
  27      * Sets the error message group (related to the vars file)
  28      * @param string group
  29      * @return void
  30      * @access public
  31      */
  32      function setGroup($Group) {
  33          $this->Group = $Group;
  34      }
  35  
  36      /**
  37      * Perform validation
  38      * @param DataSource - subclass to validate
  39      * @param ErrorList
  40      * @return boolean (always TRUE is base class)
  41      * @access protected
  42      * @abstract
  43      */
  44      function validate(&$DataSource, &$ErrorList) {
  45          RaiseError('compiler', 'ABSTRACTMETHOD',
  46                     array('method' => __FUNCTION__ .'()', 'class' => __CLASS__));
  47      }
  48  }
  49  
  50  /**
  51  * Rules responsbile for validating a single field descend from this class.
  52  * @see http://wact.sourceforge.net/index.php/SingleFieldRule
  53  * @access public
  54  * @package WACT_VALIDATION
  55  * @abstract
  56  */
  57  class SingleFieldRule extends Rule {
  58      /**
  59      * Field name to validate
  60      * @var string
  61      * @access private
  62      */
  63      var $fieldname;
  64      /**
  65      * Is this field valid?
  66      * @var boolean
  67      * @access private
  68      */
  69      var $IsValid = TRUE;
  70      /**
  71      * Error Collection Object
  72      * @var ErrorList
  73      * @access private
  74      */
  75      var $ErrorList;
  76  
  77      /**
  78      * Constructs Rule
  79      * @param string fieldname to validate
  80      * @access public
  81      */
  82      function SingleFieldRule($fieldname) {
  83          $this->fieldname = $fieldname;
  84      }
  85  
  86      /**
  87      * Returns the fieldname the rule applies to
  88      * @return string name of field
  89      * @access public
  90      */
  91      function getField() {
  92          return $this->fieldname;
  93      }
  94  
  95      /**
  96      * Signal that an error has occurred.
  97      * @param string id of the error
  98      * @param optional data regarding the error
  99      * @access protected
 100      */
 101      function Error($id, $values = NULL) {
 102          $this->IsValid = FALSE;
 103          $this->ErrorList->addError($this->Group, $id, 
 104              array('Field' => $this->fieldname), $values);
 105      }
 106  
 107      /**
 108      * Have we already determined this error to be invalid?
 109      * @param string id of the error
 110      * @param optional data regarding the error
 111      * @access protected
 112      */
 113      function IsValid() {
 114          return $this->IsValid;
 115      }
 116      
 117      /**
 118      * Perform validation
 119      * @param DataSource - Data to validate
 120      * @param ErrorList
 121      * @return boolean (always TRUE is base class)
 122      * @access public
 123      */
 124      function validate(&$DataSource, &$ErrorList) {
 125          $this->IsValid = TRUE;
 126          $this->ErrorList =& $ErrorList;
 127          $value = $DataSource->get($this->fieldname);
 128          if (isset($value) && $value !== '') {
 129              $this->Check($value);
 130          }
 131          return $this->IsValid;
 132      }
 133  
 134      /**
 135      * Check a Single Value to see if its valid
 136      * @param value - to check
 137      * @access protected
 138      * @abstract
 139      */
 140      function Check($value) {
 141          RaiseError('compiler', 'ABSTRACTMETHOD',
 142                     array('method' => __FUNCTION__ .'()', 'class' => __CLASS__));
 143      }
 144  }
 145  
 146  /**
 147  * For fields which must be supplied a value by the user
 148  * @see http://wact.sourceforge.net/index.php/RequiredRule
 149  * @access public
 150  * @package WACT_VALIDATION
 151  */
 152  class RequiredRule extends SingleFieldRule {
 153      /**
 154      * Constructs RequiredRule
 155      * @param string fieldname to validate
 156      * @access public
 157      */
 158      function RequiredRule($fieldname) {
 159          parent :: SingleFieldRule($fieldname);
 160      }
 161  
 162      /**
 163      * Performs validation
 164      * @param DataSource - data to validate
 165      * @param ErrorList
 166      * @return boolean TRUE if validation passed
 167      * @access public
 168      */
 169      function validate(&$DataSource, &$ErrorList) {
 170          $value = $DataSource->get($this->fieldname);
 171          if (!isset($value) || $value === '') {
 172              $ErrorList->addError($this->Group, 'MISSING', 
 173                  array('Field' => $this->fieldname));
 174              return FALSE;
 175          }
 176          return TRUE;
 177      }
 178  }
 179  
 180  /**
 181  * For fields have a minimum and maximum length
 182  * @see http://wact.sourceforge.net/index.php/SizeRangeRule
 183  * @access public
 184  * @package WACT_VALIDATION
 185  */
 186  class SizeRangeRule extends SingleFieldRule {
 187      /**
 188      * Minumum length
 189      * @var int
 190      * @access private
 191      */
 192      var $minLength;
 193      /**
 194      * Maximum length
 195      * @var int
 196      * @access private
 197      */
 198      var $maxLength;
 199  
 200      /**
 201      * Constructs SizeRangeRule
 202      * @param string fieldname to validate
 203      * @param int Minumum length
 204      * @param int Maximum length (optional)
 205      * @access public
 206      */
 207      function SizeRangeRule($fieldname, $minLength, $maxLength = NULL) {
 208          parent :: SingleFieldRule($fieldname);
 209          if (is_null($maxLength)) {
 210              $this->minLength = NULL;
 211              $this->maxLength = $minLength;
 212          } else {
 213              $this->minLength = $minLength;
 214              $this->maxLength = $maxLength;
 215          }
 216      }
 217  
 218      /**
 219      * Performs validation of a single value
 220      * @access protected
 221      */
 222      function Check($value) {
 223          if (!is_null($this->minLength) && (strlen($value) < $this->minLength)) {
 224              $this->Error('SIZE_TOO_SMALL', array('min' => $this->minLength));
 225          } else if (strlen($value) > $this->maxLength) {
 226              $this->Error('SIZE_TOO_BIG', array('max' => $this->maxLength));
 227          }
 228      }
 229  }
 230  
 231  ?>


Generated: Sun Nov 28 19:36:09 2004 Cross-referenced by PHPXref 0.5