示例#1
0
 public function validate($options = array())
 {
     $fieldsArray = $this->_model->getTable()->getFieldProperties();
     $fieldOptions = array();
     foreach ($fieldsArray as $fieldName => $fieldProperties) {
         $fieldOptions[$fieldName] = array_merge($fieldProperties, issetor($options[$fieldName], array()));
     }
     $valid = true;
     $errors = array();
     $this->_initValidators($fieldOptions);
     foreach ($this->_model->getValidators() as $fieldName => $fieldValidators) {
         foreach ($fieldValidators as $fieldValidator) {
             /* @var $validator Ajde_Model_ValidatorAbstract */
             $value = null;
             if ($this->_model->has($fieldName)) {
                 $value = $this->_model->get($fieldName);
             }
             $result = $fieldValidator->validate($fieldOptions[$fieldName], $value);
             if ($result['valid'] === false) {
                 if (!isset($errors[$fieldName])) {
                     $errors[$fieldName] = array();
                 }
                 $errors[$fieldName][] = $result['error'];
                 $valid = false;
             }
         }
     }
     $this->_errors = $errors;
     return $valid;
 }
示例#2
0
文件: Validator.php 项目: nabble/ajde
 public function validate($options = [])
 {
     $fieldsArray = $this->_model->getTable()->getFieldProperties();
     $fieldOptions = [];
     // Add all model fields
     foreach ($fieldsArray as $fieldName => $fieldProperties) {
         $fieldOptions[$fieldName] = array_merge($fieldProperties, isset($options[$fieldName]) ? $options[$fieldName] : []);
         if (isset($options[$fieldName])) {
             unset($options[$fieldName]);
         }
     }
     // Add all non-model fields
     foreach ($options as $fieldName => $fieldProperties) {
         $fieldOptions[$fieldName] = $fieldProperties;
     }
     $valid = true;
     $errors = [];
     $this->_initValidators($fieldOptions);
     foreach ($this->_model->getValidators() as $fieldName => $fieldValidators) {
         foreach ($fieldValidators as $fieldValidator) {
             /* @var $fieldValidator Ajde_Model_ValidatorAbstract */
             $value = null;
             if ($this->_model->has($fieldName)) {
                 $value = $this->_model->get($fieldName);
                 if (is_array($value)) {
                     $value = implode(',', $value);
                 } else {
                     $value = (string) $value;
                 }
             }
             // Only validate when dynamic field is shown
             if ($this->shouldValidateDynamicField($fieldOptions[$fieldName])) {
                 $result = $fieldValidator->validate($fieldOptions[$fieldName], $value);
                 if ($result['valid'] === false) {
                     if (!isset($errors[$fieldName])) {
                         $errors[$fieldName] = [];
                     }
                     $errors[$fieldName][] = $result['error'];
                     $valid = false;
                 }
             }
         }
     }
     $this->_errors = $errors;
     return $valid;
 }