PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/yii/framework/validators/CValidator.php

https://bitbucket.org/dfr/tehsad
PHP | 210 lines | 82 code | 14 blank | 114 comment | 12 complexity | 86dec46f8eee287b9610ff5ff9dcf677 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, BSD-2-Clause
  1. <?php
  2. /**
  3. * CValidator class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright Copyright &copy; 2008-2009 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CValidator is the base class for all validators.
  12. *
  13. * Child classes must implement the {@link validateAttribute} method.
  14. *
  15. * The following properties are defined in CValidator:
  16. * <ul>
  17. * <li>{@link attributes}: array, list of attributes to be validated;</li>
  18. * <li>{@link message}: string, the customized error message. The message
  19. * may contain placeholders that will be replaced with the actual content.
  20. * For example, the "{attribute}" placeholder will be replaced with the label
  21. * of the problematic attribute. Different validators may define additional
  22. * placeholders.</li>
  23. * <li>{@link on}: string, in which scenario should the validator be in effect.
  24. * This is used to match the 'on' parameter supplied when calling {@link CModel::validate}.</li>
  25. * </ul>
  26. *
  27. * When using {@link createValidator} to create a validator, the following aliases
  28. * are recognized as the corresponding built-in validator classes:
  29. * <ul>
  30. * <li>required: {@link CRequiredValidator}</li>
  31. * <li>filter: {@link CFilterValidator}</li>
  32. * <li>match: {@link CRegularExpressionValidator}</li>
  33. * <li>email: {@link CEmailValidator}</li>
  34. * <li>url: {@link CUrlValidator}</li>
  35. * <li>unique: {@link CUniqueValidator}</li>
  36. * <li>compare: {@link CCompareValidator}</li>
  37. * <li>length: {@link CStringValidator}</li>
  38. * <li>in: {@link CRangeValidator}</li>
  39. * <li>numerical: {@link CNumberValidator}</li>
  40. * <li>captcha: {@link CCaptchaValidator}</li>
  41. * <li>type: {@link CTypeValidator}</li>
  42. * <li>file: {@link CFileValidator}</li>
  43. * <li>default: {@link CDefaultValueValidator}</li>
  44. * <li>exist: {@link CExistValidator}</li>
  45. * <li>boolean: {@link CBooleanValidator}</li>
  46. * </ul>
  47. *
  48. * @author Qiang Xue <qiang.xue@gmail.com>
  49. * @version $Id: CValidator.php 1405 2009-09-10 19:18:30Z qiang.xue $
  50. * @package system.validators
  51. * @since 1.0
  52. */
  53. abstract class CValidator extends CComponent
  54. {
  55. /**
  56. * @var array list of built-in validators (name=>class)
  57. */
  58. public static $builtInValidators=array(
  59. 'required'=>'CRequiredValidator',
  60. 'filter'=>'CFilterValidator',
  61. 'match'=>'CRegularExpressionValidator',
  62. 'email'=>'CEmailValidator',
  63. 'url'=>'CUrlValidator',
  64. 'unique'=>'CUniqueValidator',
  65. 'compare'=>'CCompareValidator',
  66. 'length'=>'CStringValidator',
  67. 'in'=>'CRangeValidator',
  68. 'numerical'=>'CNumberValidator',
  69. 'captcha'=>'CCaptchaValidator',
  70. 'type'=>'CTypeValidator',
  71. 'file'=>'CFileValidator',
  72. 'default'=>'CDefaultValueValidator',
  73. 'exist'=>'CExistValidator',
  74. 'boolean'=>'CBooleanValidator',
  75. );
  76. /**
  77. * @var array list of attributes to be validated.
  78. */
  79. public $attributes;
  80. /**
  81. * @var string the user-defined error message. Different validators may define various
  82. * placeholders in the message that are to be replaced with actual values. All validators
  83. * recognize "{attribute}" placeholder, which will be replaced with the label of the attribute.
  84. */
  85. public $message;
  86. /**
  87. * @var array list of scenarios that the validator should be applied.
  88. * Each array value refers to a scenario name with the same name as its array key.
  89. */
  90. public $on;
  91. /**
  92. * Validates a single attribute.
  93. * This method should be overriden by child classes.
  94. * @param CModel the data object being validated
  95. * @param string the name of the attribute to be validated.
  96. */
  97. abstract protected function validateAttribute($object,$attribute);
  98. /**
  99. * Creates a validator object.
  100. * @param string the name or class of the validator
  101. * @param CModel the data object being validated that may contain the inline validation method
  102. * @param mixed list of attributes to be validated. This can be either an array of
  103. * the attribute names or a string of comma-separated attribute names.
  104. * @param array initial values to be applied to the validator properties
  105. */
  106. public static function createValidator($name,$object,$attributes,$params)
  107. {
  108. if(is_string($attributes))
  109. $attributes=preg_split('/[\s,]+/',$attributes,-1,PREG_SPLIT_NO_EMPTY);
  110. if(isset($params['on']))
  111. {
  112. if(is_array($params['on']))
  113. $on=$params['on'];
  114. else
  115. $on=preg_split('/[\s,]+/',$params['on'],-1,PREG_SPLIT_NO_EMPTY);
  116. }
  117. else
  118. $on=array();
  119. if(method_exists($object,$name))
  120. {
  121. $validator=new CInlineValidator;
  122. $validator->attributes=$attributes;
  123. $validator->method=$name;
  124. $validator->params=$params;
  125. }
  126. else
  127. {
  128. $params['attributes']=$attributes;
  129. if(isset(self::$builtInValidators[$name]))
  130. $className=Yii::import(self::$builtInValidators[$name],true);
  131. else
  132. $className=Yii::import($name,true);
  133. $validator=new $className;
  134. foreach($params as $name=>$value)
  135. $validator->$name=$value;
  136. }
  137. $validator->on=empty($on) ? array() : array_combine($on,$on);
  138. return $validator;
  139. }
  140. /**
  141. * Validates the specified object.
  142. * @param CModel the data object being validated
  143. * @param array the list of attributes to be validated. Defaults to null,
  144. * meaning every attribute listed in {@link attributes} will be validated.
  145. */
  146. public function validate($object,$attributes=null)
  147. {
  148. if(is_array($attributes))
  149. $attributes=array_intersect($this->attributes,$attributes);
  150. else
  151. $attributes=$this->attributes;
  152. foreach($attributes as $attribute)
  153. $this->validateAttribute($object,$attribute);
  154. }
  155. /**
  156. * Returns a value indicating whether the validator applies to the specified scenario.
  157. * A validator applies to a scenario as long as any of the following conditions is met:
  158. * <ul>
  159. * <li>the validator's "on" property is empty</li>
  160. * <li>the validator's "on" property contains the specified scenario</li>
  161. * </ul>
  162. * @param string scenario name
  163. * @return boolean whether the validator applies to the specified scenario.
  164. * @since 1.0.2
  165. */
  166. public function applyTo($scenario)
  167. {
  168. return empty($this->on) || isset($this->on[$scenario]);
  169. }
  170. /**
  171. * Adds an error about the specified attribute to the active record.
  172. * This is a helper method that performs message selection and internationalization.
  173. * @param CModel the data object being validated
  174. * @param string the attribute being validated
  175. * @param string the error message
  176. * @param array values for the placeholders in the error message
  177. */
  178. protected function addError($object,$attribute,$message,$params=array())
  179. {
  180. $params['{attribute}']=$object->getAttributeLabel($attribute);
  181. $object->addError($attribute,strtr($message,$params));
  182. }
  183. /**
  184. * Checks if the given value is empty.
  185. * A value is considered empty if it is null, an empty array, or the trimmed result is an empty string.
  186. * Note that this method is different from PHP empty(). It will return false when the value is 0.
  187. * @param mixed the value to be checked
  188. * @param boolean whether to perform trimming before checking if the string is empty. Defaults to false.
  189. * @return boolean whether the value is empty
  190. * @since 1.0.9
  191. */
  192. protected function isEmpty($value,$trim=false)
  193. {
  194. return $value===null || $value===array() || $value==='' || $trim && !is_array($value) && trim($value)==='';
  195. }
  196. }