mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-20 08:27:21 +08:00
Restriction on inline model method usage at yii\validators\EachValidator removed in case of usage inside model scope
This commit is contained in:
@@ -29,9 +29,8 @@ use yii\base\Model;
|
|||||||
* }
|
* }
|
||||||
* ~~~
|
* ~~~
|
||||||
*
|
*
|
||||||
* > Note: This validator will not work with inline validation rules.
|
* > Note: This validator will not work with inline validation rules in case of usage outside the model scope,
|
||||||
*
|
* e.g. via [[validate()]] method.
|
||||||
* @property Validator $validator related validator instance. This property is read only.
|
|
||||||
*
|
*
|
||||||
* @author Paul Klimov <klimov.paul@gmail.com>
|
* @author Paul Klimov <klimov.paul@gmail.com>
|
||||||
* @since 2.0.4
|
* @since 2.0.4
|
||||||
@@ -78,28 +77,33 @@ class EachValidator extends Validator
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the validator declared in [[rule]].
|
* Returns the validator declared in [[rule]].
|
||||||
|
* @param Model|null $model model in which context validator should be created.
|
||||||
* @return Validator the declared validator.
|
* @return Validator the declared validator.
|
||||||
*/
|
*/
|
||||||
public function getValidator()
|
private function getValidator($model = null)
|
||||||
{
|
{
|
||||||
if ($this->_validator === null) {
|
if ($this->_validator === null) {
|
||||||
$this->_validator = $this->createEmbeddedValidator();
|
$this->_validator = $this->createEmbeddedValidator($model);
|
||||||
}
|
}
|
||||||
return $this->_validator;
|
return $this->_validator;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates validator object based on the validation rule specified in [[rule]].
|
* Creates validator object based on the validation rule specified in [[rule]].
|
||||||
|
* @param Model|null $model model in which context validator should be created.
|
||||||
|
* @throws \yii\base\InvalidConfigException
|
||||||
* @return Validator validator instance
|
* @return Validator validator instance
|
||||||
* @throws InvalidConfigException if any validation rule configuration is invalid
|
|
||||||
*/
|
*/
|
||||||
private function createEmbeddedValidator()
|
private function createEmbeddedValidator($model)
|
||||||
{
|
{
|
||||||
$rule = $this->rule;
|
$rule = $this->rule;
|
||||||
if ($rule instanceof Validator) {
|
if ($rule instanceof Validator) {
|
||||||
return $rule;
|
return $rule;
|
||||||
} elseif (is_array($rule) && isset($rule[0])) { // validator type
|
} elseif (is_array($rule) && isset($rule[0])) { // validator type
|
||||||
return Validator::createValidator($rule[0], new Model(), $this->attributes, array_slice($rule, 1));
|
if (!is_object($model)) {
|
||||||
|
$model = new Model(); // mock up context model
|
||||||
|
}
|
||||||
|
return Validator::createValidator($rule[0], $model, $this->attributes, array_slice($rule, 1));
|
||||||
} else {
|
} else {
|
||||||
throw new InvalidConfigException('Invalid validation rule: a rule must be an array specifying validator type.');
|
throw new InvalidConfigException('Invalid validation rule: a rule must be an array specifying validator type.');
|
||||||
}
|
}
|
||||||
@@ -121,6 +125,7 @@ class EachValidator extends Validator
|
|||||||
}
|
}
|
||||||
$model->$attribute = $filteredValue;
|
$model->$attribute = $filteredValue;
|
||||||
} else {
|
} else {
|
||||||
|
$this->getValidator($model); // ensure model context while validator creation
|
||||||
parent::validateAttribute($model, $attribute);
|
parent::validateAttribute($model, $attribute);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user