From 83e26039856c4cf768041cc87321c71fd3dbf54d Mon Sep 17 00:00:00 2001 From: Klimov Paul Date: Thu, 16 Apr 2015 11:32:02 +0300 Subject: [PATCH] Restriction on inline model method usage at `yii\validators\EachValidator` removed in case of usage inside model scope --- framework/validators/EachValidator.php | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/framework/validators/EachValidator.php b/framework/validators/EachValidator.php index dd1c3914f1..90875eb638 100644 --- a/framework/validators/EachValidator.php +++ b/framework/validators/EachValidator.php @@ -29,9 +29,8 @@ use yii\base\Model; * } * ~~~ * - * > Note: This validator will not work with inline validation rules. - * - * @property Validator $validator related validator instance. This property is read only. + * > Note: This validator will not work with inline validation rules in case of usage outside the model scope, + * e.g. via [[validate()]] method. * * @author Paul Klimov * @since 2.0.4 @@ -78,28 +77,33 @@ class EachValidator extends Validator /** * Returns the validator declared in [[rule]]. + * @param Model|null $model model in which context validator should be created. * @return Validator the declared validator. */ - public function getValidator() + private function getValidator($model = null) { if ($this->_validator === null) { - $this->_validator = $this->createEmbeddedValidator(); + $this->_validator = $this->createEmbeddedValidator($model); } return $this->_validator; } /** * 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 - * @throws InvalidConfigException if any validation rule configuration is invalid */ - private function createEmbeddedValidator() + private function createEmbeddedValidator($model) { $rule = $this->rule; if ($rule instanceof Validator) { return $rule; } 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 { 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; } else { + $this->getValidator($model); // ensure model context while validator creation parent::validateAttribute($model, $attribute); } }