Answer the question
In order to leave comments, you need to log in
How to understand the following code?
There is a method
public function myRule($attribute, $params)
{
if (!in_array($this->$attribute, ['USA', 'world'])){
$this->addError($attribute, 'Страна должна быть либо "USA" или "Indonesia".');
}
}
if (!in_array($attribute, ['USA', 'world']))
Answer the question
In order to leave comments, you need to log in
What does $this->$attribute refer to in this code?
public function rules() {
return [
['country', 'myRule']
];
}
public function myRule($attribute, $params)
{
//$attribute == 'country' - поле в модели
//$this->$attribute == $this->country - значение поля в модели
if (!in_array($this->$attribute, ['USA', 'world'])){
$this->addError($attribute, 'Страна должна быть либо "USA" или "Indonesia".');
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question