[ Index ] |
|
Code source de LifeType 1.2.4 |
1 <?php 2 3 /** 4 * \defgroup Validator 5 * 6 * Validator in pLog is an implementation of the 'Strategy' pattern as it can be seen 7 * http://www.phppatterns.com/index.php/article/articleview/13/1/1/. 8 * 9 * Validator classes allow to reuse validation logic wherever needed, and in a uniform 10 * way since class users can safely assume that all validators implement the 11 * Validator::validate() method. 12 * 13 * Validator classes in pLog can be built as a set of Rules, as a set of chained Validator 14 * classes or they can also bring their own non-reusable logic (non-reusable in the sense that 15 * validation logic does not necessarily need to be part of a Rule class) 16 * 17 * This the preferred way to validate data using a Validator class in pLog: 18 * 19 * <pre> 20 * $val = new UsernameValidator(); 21 * if( !$val->validate( $newUsername )) 22 * print( "the username $newUsername is not correct!" ); 23 * else 24 * print( "the username is correct" ); 25 * </pre> 26 * 27 * It is also possible to implemlent our own custom validators by extending the Validator base class 28 * and adding a few rules. Keep in mind that if we overwrite the Validator::validate() method, 29 * we will lose some logic regarding rules. If our Validator class does not use rules, it is safe 30 * to overwrite such method and return 'true' if successful or false otherwise. 31 * 32 * In order to implement our own validator, the preferred way of doing it is by adding the necessary 33 * Validator and Rule objects in the constructor: 34 * 35 * <pre> 36 * class NewValidator extend Validator { 37 * function NewValidator() 38 * { 39 * $this->addRule( new NonEmptyRule()); 40 * $this->addRule( new NumericRule()); 41 * } 42 * } 43 * </pre> 44 */ 45 46 lt_include( PLOG_CLASS_PATH."class/data/validator/validation.class.php" ); 47 48 /** 49 * \ingroup Validator 50 * 51 * Base class that all other validators extend. See the documentation of the Validator module for more 52 * information. 53 */ 54 class Validator extends Validation 55 { 56 57 var $_rules; 58 59 /** 60 * Initializes the constructor 61 */ 62 function Validator() 63 { 64 $this->Validation(); 65 66 $this->_rules = array(); 67 } 68 69 /** 70 * Adds a rule to the Validator 71 * 72 * @param rule A valid Rule object 73 */ 74 function addRule(&$rule) 75 { 76 $this->_rules[] = &$rule; 77 } 78 79 /** 80 * Our validators can also be built based on other validators. It is also possible 81 * to use the ChainedValidator if we don't wish to create a completely new Validator 82 * 83 * @param validator A Validator object or a class extending from it 84 */ 85 function addValidator(&$validator) 86 { 87 foreach ($validator->_rules as $rule) 88 { 89 $this->addRule($rule); 90 } 91 } 92 93 /** 94 * This is the main method that takes care of processing all the rules. It is not necessary 95 * to reimplement this method in our custom Validator classes if we have already added some rules, 96 * as this method already provides some logic for going through the rules and setting some extended 97 * error codes if necessary. 98 * 99 * If your custom validator does not use rules, it is safe to reimplement this method. 100 * 101 * @param value The value that we're going to validate. 102 */ 103 function validate($value) 104 { 105 foreach ($this->_rules as $rule) 106 { 107 if (!$rule->validate($value)) 108 { 109 $this->_setError($rule->getError()); 110 return false; 111 } 112 } 113 114 return true; 115 } 116 } 117 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Mon Nov 26 21:04:15 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |