Пример #1
0
 public function validate()
 {
     $this->_errors = array();
     $type = $this->_model->isNewRecord() ? 'create' : 'update';
     if (empty($this->_registeredValidates[$type])) {
         return true;
     }
     if ($this->_model->callback->call("before_validation_on_{$type}") === false) {
         return false;
     }
     $validates = $this->_registeredValidates[$type];
     foreach ($validates as $validate) {
         if ($validate['type'] === 'validate') {
             if (method_exists($this->_model, $validate[0])) {
                 $result = Reflection::invokeMethod($this->_model, $validate[0]);
             }
             continue;
         }
         $valid = true;
         $attribute = $validate[0];
         $value = $this->_model->{$attribute};
         $valueLength = strlen($value);
         if ($valueLength <= 0 && $validate['type'] !== 'validates_presence_of') {
             continue;
         }
         switch ($validate['type']) {
             case 'validates_presence_of':
                 $valid = $valueLength > 0;
                 break;
             case 'validates_size_of':
                 $sizeType = $this->_getSizeType($validate);
                 if ($sizeType !== null) {
                     switch ($sizeType) {
                         case 'is':
                             $valid = $valueLength === $validate['is'];
                             break;
                         case 'in':
                             $valid = $valueLength >= $validate['in'][0] && $valueLength <= $validate['in'][1];
                             break;
                         case 'maximum':
                             $valid = $valueLength <= $validate['maximum'];
                             break;
                         case 'minimum':
                             $valid = $valueLength >= $validate['minimum'];
                             break;
                     }
                 }
                 break;
             case 'validates_inclusion_of':
                 $valid = in_array($value, $validate['in']);
                 break;
             case 'validates_exclusion_of':
                 $valid = in_array($value, $validate['in']) === false;
                 break;
             case 'validates_format_of':
                 $valid = preg_match($validate['with'], $value) ? true : false;
                 break;
             case 'validates_numericality_of':
                 $numericalityType = $this->_getNumericalityType($validate);
                 if ($numericalityType !== null) {
                     $value = floatval($value);
                     switch ($numericalityType) {
                         case 'only_integer':
                             $valid = preg_match('/^-?\\d+$/', $value) ? true : false;
                             break;
                         case 'even':
                             $valid = $value % 2 === 0;
                             break;
                         case 'odd':
                             $valid = $value % 2 === 1;
                             break;
                         case 'greater':
                             $valid = $value > $validate['greater'];
                             break;
                         case 'greater_or_equal':
                             $valid = $value >= $validate['greater_or_equal'];
                             break;
                         case 'equal':
                             $valid = $value === $validate['equal'];
                             break;
                         case 'less':
                             $valid = $value < $validate['less'];
                             break;
                         case 'less_or_equal':
                             $valid = $value <= $validate['less_or_equal'];
                             break;
                     }
                 }
                 break;
             case 'validates_uniqueness_of':
                 $options = array('conditions' => array("`{$attribute}` = ? AND `id` != ?", $value, $this->_model->id));
                 $valid = $this->_model->exists($options) === false;
                 break;
         }
         if ($valid === true) {
             continue;
         }
         $error = $this->_getErrorMessage($validate, $value);
         if ($error === null) {
             continue;
         }
         $this->addError($attribute, $error);
     }
     $this->_model->callback->call("after_validation_on_{$type}");
     return empty($this->_errors);
 }