mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-11 19:20:01 +08:00
Fix #18051: Fix missing support for custom validation method in EachValidator
This commit is contained in:
@@ -8,6 +8,7 @@ Yii Framework 2 Change Log
|
|||||||
- Enh #18019: Allow jQuery 3.5.0 to be installed (wouter90)
|
- Enh #18019: Allow jQuery 3.5.0 to be installed (wouter90)
|
||||||
- Bug #18026: Fix `ArrayHelper::getValue()` did not work with `ArrayAccess` objects (mikk150)
|
- Bug #18026: Fix `ArrayHelper::getValue()` did not work with `ArrayAccess` objects (mikk150)
|
||||||
- Enh #18048: Use `Instance::ensure()` to set `User::$accessChecker` (lav45)
|
- Enh #18048: Use `Instance::ensure()` to set `User::$accessChecker` (lav45)
|
||||||
|
- Bug #18051: Fix missing support for custom validation method in EachValidator (bizley)
|
||||||
|
|
||||||
|
|
||||||
2.0.35 May 02, 2020
|
2.0.35 May 02, 2020
|
||||||
|
|||||||
@@ -209,10 +209,13 @@ class Validator extends Component
|
|||||||
{
|
{
|
||||||
$params['attributes'] = $attributes;
|
$params['attributes'] = $attributes;
|
||||||
|
|
||||||
if ($type instanceof \Closure || ($model->hasMethod($type) && !isset(static::$builtInValidators[$type]))) {
|
if ($type instanceof \Closure) {
|
||||||
// method-based validator
|
|
||||||
$params['class'] = __NAMESPACE__ . '\InlineValidator';
|
$params['class'] = __NAMESPACE__ . '\InlineValidator';
|
||||||
$params['method'] = $type;
|
$params['method'] = $type;
|
||||||
|
} elseif (!isset(static::$builtInValidators[$type]) && $model->hasMethod($type)) {
|
||||||
|
// method-based validator
|
||||||
|
$params['class'] = __NAMESPACE__ . '\InlineValidator';
|
||||||
|
$params['method'] = [$model, $type];
|
||||||
} else {
|
} else {
|
||||||
if (isset(static::$builtInValidators[$type])) {
|
if (isset(static::$builtInValidators[$type])) {
|
||||||
$type = static::$builtInValidators[$type];
|
$type = static::$builtInValidators[$type];
|
||||||
|
|||||||
@@ -49,4 +49,9 @@ class Speaker extends Model
|
|||||||
'duplicates' => ['firstName', 'firstName', '!underscore_style', '!underscore_style'],
|
'duplicates' => ['firstName', 'firstName', '!underscore_style', '!underscore_style'],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function customValidatingMethod($attribute, $params, $validator)
|
||||||
|
{
|
||||||
|
$this->addError($attribute, 'Custom method error');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -238,4 +238,15 @@ class EachValidatorTest extends TestCase
|
|||||||
$this->assertEquals('This is the custom label must be a valid IP address.', $model->getFirstError('customLabel'));
|
$this->assertEquals('This is the custom label must be a valid IP address.', $model->getFirstError('customLabel'));
|
||||||
$this->assertEquals('First Name is invalid.', $model->getFirstError('firstName'));
|
$this->assertEquals('First Name is invalid.', $model->getFirstError('firstName'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testCustomMethod()
|
||||||
|
{
|
||||||
|
$model = new Speaker();
|
||||||
|
$model->firstName = ['a', 'b'];
|
||||||
|
|
||||||
|
$validator = new EachValidator(['rule' => ['customValidatingMethod']]);
|
||||||
|
$validator->validateAttribute($model, 'firstName');
|
||||||
|
|
||||||
|
$this->assertEquals('Custom method error', $model->getFirstError('firstName'));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ class ValidatorTest extends TestCase
|
|||||||
$this->assertSame(['c', 'd', 'e'], $val->except);
|
$this->assertSame(['c', 'd', 'e'], $val->except);
|
||||||
$val = TestValidator::createValidator('inlineVal', $model, ['val_attr_a'], ['params' => ['foo' => 'bar']]);
|
$val = TestValidator::createValidator('inlineVal', $model, ['val_attr_a'], ['params' => ['foo' => 'bar']]);
|
||||||
$this->assertInstanceOf(InlineValidator::className(), $val);
|
$this->assertInstanceOf(InlineValidator::className(), $val);
|
||||||
$this->assertSame('inlineVal', $val->method);
|
$this->assertSame('inlineVal', $val->method[1]);
|
||||||
$this->assertSame(['foo' => 'bar'], $val->params);
|
$this->assertSame(['foo' => 'bar'], $val->params);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user