Commit 0ed6355e by Carsten Brandt

test and docs to be clear about #4558

parent 2ffd6d8e
......@@ -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);
}
......
......@@ -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'; }],
];
}
}
......@@ -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()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment