Ejemplo n.º 1
0
 public function testMixed()
 {
     $validator = new ArkValidator(array('username' => 'hello', 'password' => '12qeer311', 'email' => '*****@*****.**', 'repeat_password' => '12qeer311', 'gender' => 'm', 'age' => '30'), array('username' => 'required|alpha_dash', 'password' => 'required|between_length:6,20', 'email' => 'required|email', 'repeat_password' => 'required|same:password', 'gender' => 'required|in:m,f', 'age' => 'required|int|between:13,100'));
     $this->assertTrue($validator->valid());
     $validator = new ArkValidator(array('username' => 'hello', 'password' => '12qeer311', 'email' => '*****@*****.**', 'repeat_password' => '12qeer311', 'gender' => 't', 'age' => 'ttt'), array('username' => 'required|alpha_dash', 'password' => 'required|between_length:6,20', 'email' => 'required|email', 'repeat_password' => 'required|same:password', 'gender' => 'required|in:m,f', 'age' => 'required|int|between:13,100'));
     $errors = $validator->getErrors();
     $this->assertTrue($errors->has('gender'));
     $this->assertTrue($errors->has('age'));
     $this->assertContains('gender', $errors->first('gender'));
     $this->assertContains('age', $errors->first('age'));
 }
Ejemplo n.º 2
0
 /**
  * Validate only one time, loop each rules and find all errors.
  */
 public function validateOnce()
 {
     if (!isset($this->validateResult)) {
         $this->validateResult = array();
         $rules = array_merge(self::$defaultCustomRules, $this->customRules);
         $ruleMessages = array_merge(self::$defaultRuleMessages, $this->ruleMessages);
         $fieldMessages = $this->fieldMessages;
         foreach ($this->dataRules as $name => $dataRule) {
             if (!is_array($dataRule)) {
                 $dataRule = explode('|', $dataRule);
             }
             $checkRequired = false;
             foreach ($dataRule as $key => $value) {
                 // rule name as key
                 if (!is_int($key)) {
                     $ruleName = $key;
                     $options = $value;
                 } else {
                     $parts = explode(':', $value, 2);
                     $ruleName = $parts[0];
                     $options = isset($parts[1]) ? $parts[1] : null;
                 }
                 // check required
                 if (!$checkRequired) {
                     $checkRequired = true;
                     if ($ruleName !== 'required') {
                         // not required, so ignore following rules
                         if (!isset($this->data[$name]) || $this->data[$name] === '' || trim($this->data[$name]) === '') {
                             break;
                         }
                     }
                 }
                 if (isset($rules[$ruleName])) {
                     $rule = $rules[$ruleName];
                 } else {
                     $methodName = str_replace(' ', '', ucfirst(str_replace('_', ' ', $ruleName)));
                     $rule = 'ArkValidator::is' . $methodName;
                 }
                 if (!isset($this->data[$name]) || !call_user_func($rule, $this->data, $name, $options)) {
                     // find template
                     $k1 = $name . '.' . $ruleName;
                     $k2 = $name;
                     if (isset($fieldMessages[$k1])) {
                         $template = $fieldMessages[$k1];
                     } elseif (isset($fieldMessages[$k2])) {
                         $template = $fieldMessages[$k2];
                     } elseif (isset($ruleMessages[$ruleName])) {
                         $template = $ruleMessages[$ruleName];
                     } else {
                         $template = ':field is not valid';
                     }
                     $params = array_merge(array(':field' => $name, ':rule' => $ruleName, ':options' => $options, ':value' => $this->data[$name]), self::$params);
                     self::$params = array();
                     // format message
                     $this->validateResult[$name][$key] = strtr($template, $params);
                 }
             }
         }
     }
 }