示例#1
0
文件: Table.php 项目: mediasadc/alba
 /**
  * validateField
  *
  * @param string $name
  * @param string $value
  * @param Doctrine_Record $record
  * @return Doctrine_Validator_ErrorStack $errorStack
  */
 public function validateField($fieldName, $value, Doctrine_Record $record = null)
 {
     if ($record instanceof Doctrine_Record) {
         $errorStack = $record->getErrorStack();
     } else {
         $record = $this->create();
         $errorStack = new Doctrine_Validator_ErrorStack($this->getOption('name'));
     }
     if ($value === self::$_null) {
         $value = null;
     } else {
         if ($value instanceof Doctrine_Record && $value->exists()) {
             $value = $value->getIncremented();
         } else {
             if ($value instanceof Doctrine_Record && !$value->exists()) {
                 foreach ($this->getRelations() as $relation) {
                     if ($fieldName == $relation->getLocalFieldName() && (get_class($value) == $relation->getClass() || is_subclass_of($value, $relation->getClass()))) {
                         return $errorStack;
                     }
                 }
             }
         }
     }
     $dataType = $this->getTypeOf($fieldName);
     // Validate field type, if type validation is enabled
     if ($this->getAttribute(Doctrine::ATTR_VALIDATE) & Doctrine::VALIDATE_TYPES) {
         if (!Doctrine_Validator::isValidType($value, $dataType)) {
             $errorStack->add($fieldName, 'type');
         }
         if ($dataType == 'enum') {
             $enumIndex = $this->enumIndex($fieldName, $value);
             if ($enumIndex === false && $value !== null) {
                 $errorStack->add($fieldName, 'enum');
             }
         }
     }
     // Validate field length, if length validation is enabled
     if ($this->getAttribute(Doctrine::ATTR_VALIDATE) & Doctrine::VALIDATE_LENGTHS) {
         if (!Doctrine_Validator::validateLength($value, $dataType, $this->getFieldLength($fieldName))) {
             $errorStack->add($fieldName, 'length');
         }
     }
     // Run all custom validators
     foreach ($this->getFieldValidators($fieldName) as $validatorName => $args) {
         if (!is_string($validatorName)) {
             $validatorName = $args;
             $args = array();
         }
         $validator = Doctrine_Validator::getValidator($validatorName);
         $validator->invoker = $record;
         $validator->field = $fieldName;
         $validator->args = $args;
         if (!$validator->validate($value)) {
             $errorStack->add($fieldName, $validator);
         }
     }
     return $errorStack;
 }
示例#2
0
 /**
  * Validates a given field using table ATTR_VALIDATE rules.
  * @see Doctrine_Core::ATTR_VALIDATE
  *
  * @param string $fieldName
  * @param string $value
  * @param Doctrine_Record $record   record to consider; if it does not exists, it is created
  * @return Doctrine_Validator_ErrorStack $errorStack
  */
 public function validateField($fieldName, $value, Doctrine_Record $record = null)
 {
     if ($record instanceof Doctrine_Record) {
         $errorStack = $record->getErrorStack();
     } else {
         $record = $this->create();
         $errorStack = new Doctrine_Validator_ErrorStack($this->getOption('name'));
     }
     if ($value === self::$_null) {
         $value = null;
     } else {
         if ($value instanceof Doctrine_Record && $value->exists()) {
             $value = $value->getIncremented();
         } else {
             if ($value instanceof Doctrine_Record && !$value->exists()) {
                 foreach ($this->getRelations() as $relation) {
                     if ($fieldName == $relation->getLocalFieldName() && (get_class($value) == $relation->getClass() || is_subclass_of($value, $relation->getClass()))) {
                         return $errorStack;
                     }
                 }
             } elseif (array_key_exists($fieldName, $this->getRelationIdentifiers()) && $record !== null) {
                 $r = $this->_relationIdentifiers[$fieldName];
                 // Related record is not saved yet
                 if (!$record->hasReference($r) || !$record->{$r}->exists()) {
                     return $errorStack;
                 }
             }
         }
     }
     $dataType = $this->getTypeOf($fieldName);
     // Validate field type, if type validation is enabled
     if ($this->getAttribute(Doctrine_Core::ATTR_VALIDATE) & Doctrine_Core::VALIDATE_TYPES) {
         if (!Doctrine_Validator::isValidType($value, $dataType)) {
             $errorStack->add($fieldName, 'type');
         }
         if ($dataType == 'enum') {
             $enumIndex = $this->enumIndex($fieldName, $value);
             if ($enumIndex === false && $value !== null) {
                 $errorStack->add($fieldName, 'enum');
             }
         }
         if ($dataType == 'set') {
             $values = $this->_columns[$fieldName]['values'];
             // Convert string to array
             if (is_string($value)) {
                 $value = explode(',', $value);
                 foreach ($value as &$v) {
                     $v = trim($v);
                 }
                 $record->set($fieldName, $value);
             }
             // Make sure each set value is valid
             foreach ($value as $k => $v) {
                 if (!in_array($v, $values)) {
                     $errorStack->add($fieldName, 'set');
                 }
             }
         }
     }
     // Validate field length, if length validation is enabled
     if ($this->getAttribute(Doctrine_Core::ATTR_VALIDATE) & Doctrine_Core::VALIDATE_LENGTHS) {
         if (!Doctrine_Validator::validateLength($value, $dataType, $this->getFieldLength($fieldName))) {
             $errorStack->add($fieldName, 'length');
         }
     }
     // Run all custom validators
     foreach ($this->getFieldValidators($fieldName) as $validatorName => $args) {
         if (!is_string($validatorName)) {
             $validatorName = $args;
             $args = array();
         }
         $validator = Doctrine_Validator::getValidator($validatorName);
         $validator->invoker = $record;
         $validator->field = $fieldName;
         $validator->args = $args;
         if (!$validator->validate($value)) {
             $errorStack->add($fieldName, $validator);
         }
     }
     return $errorStack;
 }