Advertisement
Guest User

Yii ArrayValidator

a guest
Feb 29th, 2012
1,135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.11 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Original sources and separateParams example:
  4.  * @see http://www.yiiframework.com/extension/array-validator/
  5.  *
  6.  * @author Marco van 't Wout, Tremani 2012
  7.  *
  8.  * Example usage:
  9.  *
  10.  * public function rules() {
  11.  *     return array(
  12.  *         array('numberList', 'ArrayValidator', 'validator'=>'numerical', 'params'=>array(
  13.  *             'integerOnly'=>true, 'allowEmpty'=>false
  14.  *         )),
  15.  *     );
  16.  * }
  17.  */
  18. class ArrayValidator extends CValidator {
  19.  
  20.     /**
  21.      * @var string name of the validator class (example: 'numerical' or 'CustomValidator')
  22.      */
  23.     public $validator;
  24.  
  25.     /**
  26.      * @var array parameters passed to the validator class
  27.      */
  28.     public $params;
  29.  
  30.     /**
  31.      * @var bool use a separate params array depending on array attribute keys
  32.      */
  33.     public $separateParams = false;
  34.  
  35.     /**
  36.      * @var boolean whether the attribute value can be null or empty. Defaults to true,
  37.      * meaning that if the attribute is empty, it is considered valid.
  38.      */
  39.     public $allowEmpty = true;
  40.  
  41.     /**
  42.      * @var Object the validator instance
  43.      */
  44.     protected $validatorObject;
  45.  
  46.     /**
  47.      * Validates the attribute of the object.
  48.      * If there is any error, the error message is added to the object.
  49.      * @param CModel $object the object being validated
  50.      * @param string $attribute the attribute being validated
  51.      */
  52.     protected function validateAttribute($object, $attribute)
  53.     {
  54.         if ($this->isEmpty($object->$attribute)) {
  55.             if ($this->allowEmpty === true) {
  56.                 $object->$attribute = null;
  57.                 return;
  58.             }
  59.             $this->addError($object, $attribute, Yii::t('', '{attribute} is not allowed to be empty', array('{attribute}'=>$attribute)));
  60.             return;
  61.         }
  62.  
  63.         if (!is_array($object->$attribute) ) {
  64.             $this->addError($object, $attribute, Yii::t('', 'You are trying to validate a non-array attribute'));
  65.             return;
  66.         }
  67.  
  68.         // Create validator and set params
  69.         $this->validatorObject = self::createValidator($this->validator, $object, array($attribute));
  70.         if (!$this->separateParams) {
  71.             $this->setValidatorParams($this->params);
  72.         }
  73.  
  74.         // Loop validator for every array element
  75.         $attributeArray = $object->$attribute; // create copy of attribute array
  76.         foreach($attributeArray as $key => &$value) { // by reference
  77.             $object->$attribute = $value; // temporary store single value in object attribute
  78.             if ($this->separateParams) {
  79.                 $this->setValidatorParams($this->params[$key]);
  80.             }
  81.             $this->validatorObject->validate($object);
  82.             $value = $object->$attribute; // put validated value back in attribute array
  83.         }
  84.         $object->$attribute = $attributeArray; // restore attribute array
  85.  
  86.         // If attribute has errors, show first error
  87.         if ($object->hasErrors($attribute)) {
  88.             $firstError = $object->errors[$attribute][0];
  89.             $object->clearErrors($attribute);
  90.             $object->addError($attribute, Yii::t('', 'At least one element has an error:').' '.$firstError);
  91.         }
  92.     }
  93.  
  94.     /**
  95.      * Set parameters for validator.
  96.      * @param array $params
  97.      */
  98.     protected function setValidatorParams($params)
  99.     {
  100.         foreach($params as $paramName => $paramValue) {
  101.             $this->validatorObject->$paramName = $paramValue;
  102.         }
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement