From 452fe3e3b02400b3b495ef781b8cf981e5ef874e Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Mon, 1 Jul 2013 19:39:56 -0400 Subject: [PATCH] delay validation of scenario to validate(). --- framework/yii/base/Model.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/framework/yii/base/Model.php b/framework/yii/base/Model.php index 462b31d..8b0d455 100644 --- a/framework/yii/base/Model.php +++ b/framework/yii/base/Model.php @@ -249,9 +249,16 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess * validation rules should be validated. * @param boolean $clearErrors whether to call [[clearErrors()]] before performing validation * @return boolean whether the validation is successful without any error. + * @throws InvalidParamException if the current scenario is unknown. */ public function validate($attributes = null, $clearErrors = true) { + $scenarios = $this->scenarios(); + $scenario = $this->getScenario(); + if (!isset($scenarios[$scenario])) { + throw new InvalidParamException("Unknown scenario: $scenario"); + } + if ($clearErrors) { $this->clearErrors(); } @@ -580,16 +587,12 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess /** * Sets the scenario for the model. + * Note that this method does not check if the scenario exists or not. + * The method [[validate()]] will perform this check. * @param string $value the scenario that this model is in. - * @throws InvalidParamException if the scenario to be set is unknown - * @see scenarios() */ public function setScenario($value) { - $scenarios = $this->scenarios(); - if (!isset($scenarios[$value])) { - throw new InvalidParamException("Setting unknown scenario: $value"); - } $this->_scenario = $value; }