mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-26 14:26:54 +08:00
Fixed yii\validators\EachValidator
does not invoke validateAttribute()
method of the embedded validator
This commit is contained in:
@ -4,6 +4,7 @@ Yii Framework 2 Change Log
|
|||||||
2.0.8 under development
|
2.0.8 under development
|
||||||
-----------------------
|
-----------------------
|
||||||
|
|
||||||
|
- Bug #9935: Fixed `yii\validators\EachValidator` does not invoke `validateAttribute()` method of the embedded validator (klimov-paul)
|
||||||
- Bug #11270: Fixed `BaseActiveRecord::link()` method in order to support closure in `indexBy` for relations declaration (iushev)
|
- Bug #11270: Fixed `BaseActiveRecord::link()` method in order to support closure in `indexBy` for relations declaration (iushev)
|
||||||
- Bug #11262: Enabled use of yii2 inside of PHAR packaged console applications (hiqsol)
|
- Bug #11262: Enabled use of yii2 inside of PHAR packaged console applications (hiqsol)
|
||||||
- Bug #11196: Fixed VarDumper throws PHP Fatal when dumping `__PHP_Incomplete_Class` (DamianZ)
|
- Bug #11196: Fixed VarDumper throws PHP Fatal when dumping `__PHP_Incomplete_Class` (DamianZ)
|
||||||
|
@ -115,19 +115,34 @@ class EachValidator extends Validator
|
|||||||
public function validateAttribute($model, $attribute)
|
public function validateAttribute($model, $attribute)
|
||||||
{
|
{
|
||||||
$value = $model->$attribute;
|
$value = $model->$attribute;
|
||||||
$validator = $this->getValidator();
|
if (!is_array($value)) {
|
||||||
if ($validator instanceof FilterValidator && is_array($value)) {
|
$this->addError($model, $attribute, $this->message, []);
|
||||||
$filteredValue = [];
|
return;
|
||||||
foreach ($value as $k => $v) {
|
|
||||||
if (!$validator->skipOnArray || !is_array($v)) {
|
|
||||||
$filteredValue[$k] = call_user_func($validator->filter, $v);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$model->$attribute = $filteredValue;
|
|
||||||
} else {
|
|
||||||
$this->getValidator($model); // ensure model context while validator creation
|
|
||||||
parent::validateAttribute($model, $attribute);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$validator = $this->getValidator($model); // ensure model context while validator creation
|
||||||
|
|
||||||
|
$originalErrors = $model->getErrors($attribute);
|
||||||
|
$filteredValue = [];
|
||||||
|
foreach ($value as $k => $v) {
|
||||||
|
$model->$attribute = $v;
|
||||||
|
$validator->validateAttribute($model, $attribute);
|
||||||
|
$filteredValue[$k] = $model->$attribute;
|
||||||
|
if ($model->hasErrors($attribute)) {
|
||||||
|
$validationErrors = $model->getErrors($attribute);
|
||||||
|
$model->clearErrors($attribute);
|
||||||
|
if (!empty($originalErrors)) {
|
||||||
|
$model->addErrors([$attribute => $originalErrors]);
|
||||||
|
}
|
||||||
|
if ($this->allowMessageFromRule) {
|
||||||
|
$model->addErrors([$attribute => $validationErrors]);
|
||||||
|
} else {
|
||||||
|
$this->addError($model, $attribute, $this->message, ['value' => $v]);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$model->$attribute = $filteredValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -108,4 +108,36 @@ class EachValidatorTest extends TestCase
|
|||||||
$validator = new EachValidator(['rule' => ['integer', 'skipOnEmpty' => false]]);
|
$validator = new EachValidator(['rule' => ['integer', 'skipOnEmpty' => false]]);
|
||||||
$this->assertFalse($validator->validate(['']));
|
$this->assertFalse($validator->validate(['']));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see https://github.com/yiisoft/yii2/issues/9935
|
||||||
|
*
|
||||||
|
* @depends testValidate
|
||||||
|
*/
|
||||||
|
public function testCompare()
|
||||||
|
{
|
||||||
|
$model = FakedValidationModel::createWithAttributes([
|
||||||
|
'attr_one' => [
|
||||||
|
'value1',
|
||||||
|
'value2',
|
||||||
|
'value3',
|
||||||
|
],
|
||||||
|
'attr_two' => 'value2',
|
||||||
|
]);
|
||||||
|
$validator = new EachValidator(['rule' => ['compare', 'compareAttribute' => 'attr_two']]);
|
||||||
|
$validator->validateAttribute($model, 'attr_one');
|
||||||
|
$this->assertNotEmpty($model->getErrors('attr_one'));
|
||||||
|
|
||||||
|
$model = FakedValidationModel::createWithAttributes([
|
||||||
|
'attr_one' => [
|
||||||
|
'value1',
|
||||||
|
'value2',
|
||||||
|
'value3',
|
||||||
|
],
|
||||||
|
'attr_two' => 'value4',
|
||||||
|
]);
|
||||||
|
$validator = new EachValidator(['rule' => ['compare', 'compareAttribute' => 'attr_two', 'operator' => '!=']]);
|
||||||
|
$validator->validateAttribute($model, 'attr_one');
|
||||||
|
$this->assertEmpty($model->getErrors('attr_one'));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user