From 0ed6355ee1cd33cd4df653271bb8541241a1f93b Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Fri, 1 Aug 2014 15:35:23 +0200 Subject: [PATCH] test and docs to be clear about #4558 --- framework/base/Model.php | 11 ++++++----- tests/unit/data/base/Singer.php | 2 ++ tests/unit/framework/base/ModelTest.php | 9 ++++++++- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/framework/base/Model.php b/framework/base/Model.php index aaf3d5a399..de8a024a1f 100644 --- a/framework/base/Model.php +++ b/framework/base/Model.php @@ -385,7 +385,6 @@ class Model extends Component implements IteratorAggregate, ArrayAccess, Arrayab if ($this->_validators === null) { $this->_validators = $this->createValidators(); } - return $this->_validators; } @@ -404,7 +403,6 @@ class Model extends Component implements IteratorAggregate, ArrayAccess, Arrayab $validators[] = $validator; } } - return $validators; } @@ -427,7 +425,6 @@ class Model extends Component implements IteratorAggregate, ArrayAccess, Arrayab throw new InvalidConfigException('Invalid validation rule: a rule must specify both attribute names and validator type.'); } } - return $validators; } @@ -436,6 +433,12 @@ class Model extends Component implements IteratorAggregate, ArrayAccess, Arrayab * This is determined by checking if the attribute is associated with a * [[\yii\validators\RequiredValidator|required]] validation rule in the * current [[scenario]]. + * + * Note that when the validator has a conditional validation applied using + * [[\yii\validators\RequiredValidator::$when|$when]] this method will return + * `false` regardless of the `when` condition because it may be called be + * before the model is loaded with data. + * * @param string $attribute attribute name * @return boolean whether the attribute is required */ @@ -446,7 +449,6 @@ class Model extends Component implements IteratorAggregate, ArrayAccess, Arrayab return true; } } - return false; } @@ -482,7 +484,6 @@ class Model extends Component implements IteratorAggregate, ArrayAccess, Arrayab public function getAttributeLabel($attribute) { $labels = $this->attributeLabels(); - return isset($labels[$attribute]) ? $labels[$attribute] : $this->generateAttributeLabel($attribute); } diff --git a/tests/unit/data/base/Singer.php b/tests/unit/data/base/Singer.php index 0891690d6b..75989bb0c2 100644 --- a/tests/unit/data/base/Singer.php +++ b/tests/unit/data/base/Singer.php @@ -10,6 +10,7 @@ class Singer extends Model { public $firstName; public $lastName; + public $test; public function rules() { @@ -17,6 +18,7 @@ class Singer extends Model [['lastName'], 'default', 'value' => 'Lennon'], [['lastName'], 'required'], [['underscore_style'], 'yii\captcha\CaptchaValidator'], + [['test'], 'required', 'when' => function($model) { return $model->firstName === 'cebe'; }], ]; } } diff --git a/tests/unit/framework/base/ModelTest.php b/tests/unit/framework/base/ModelTest.php index f64afd7ed3..6a2cd2aabe 100644 --- a/tests/unit/framework/base/ModelTest.php +++ b/tests/unit/framework/base/ModelTest.php @@ -216,7 +216,7 @@ class ModelTest extends TestCase public function testDefaultScenarios() { $singer = new Singer(); - $this->assertEquals(['default' => ['lastName', 'underscore_style']], $singer->scenarios()); + $this->assertEquals(['default' => ['lastName', 'underscore_style', 'test']], $singer->scenarios()); $scenarios = [ 'default' => ['id', 'name', 'description'], @@ -238,6 +238,13 @@ class ModelTest extends TestCase $singer = new Singer(); $this->assertFalse($singer->isAttributeRequired('firstName')); $this->assertTrue($singer->isAttributeRequired('lastName')); + + // attribute is not marked as required when a conditional validation is applied using `$when`. + // the condition should not be applied because this info may be retrieved before model is loaded with data + $singer->firstName = 'qiang'; + $this->assertFalse($singer->isAttributeRequired('test')); + $singer->firstName = 'cebe'; + $this->assertFalse($singer->isAttributeRequired('test')); } public function testCreateValidators()