The value being compared with can be another attribute value (specified via [[compareAttribute]]) or a constant (specified via [[compareValue]]. When both are specified, the latter takes precedence. If neither is specified, the attribute will be compared with another attribute whose name is by appending "_repeat" to the source attribute name. CompareValidator supports different comparison operators, specified via the [[operator]] property. The default comparison function is based on string values, which means the values are compared byte by byte. When comparing numbers, make sure to set the [[$type]] to [[TYPE_NUMBER]] to enable numeric comparison.
부터: 2.0
저자: Qiang Xue (qiang.xue@gmail.com)
상속: extends Validator
예제 #1
0
 /**
  * @param $attribute
  */
 public function validatePassword($attribute)
 {
     if (empty($this->password)) {
         return;
     }
     $StringValidator = new StringValidator(['min' => 6]);
     $StringValidator->validateAttribute($this, 'password');
     $CompareValidator = new CompareValidator(['compareAttribute' => 'password']);
     $CompareValidator->validateAttribute($this, 'repassword');
 }
예제 #2
0
 public function testValidateAttributeOperators()
 {
     $value = 55;
     foreach ($this->getOperationTestData($value) as $operator => $tests) {
         $val = new CompareValidator(['operator' => $operator, 'compareValue' => $value]);
         foreach ($tests as $test) {
             $model = new FakedValidationModel();
             $model->attr_test = $test[0];
             $val->validateAttribute($model, 'attr_test');
             $this->assertEquals($test[1], !$model->hasErrors('attr_test'));
         }
     }
 }