There is such code:
public function rules() { return [ ['text', 'isLogged'], [['text'], 'required', 'message' => 'Заполните это поле'], ['text', 'string','min' => 6, 'tooShort' => 'Слишком короткое сообщение'], ]; } public function isLogged($attribute) { if (!$this->hasErrors()) { if (Yii::$app->user->isGuest) $this->addError($attribute, 'Необходимо авторизироваться'); } } Everything works except for the isLogged function. What I have just not tried, different variations of the addition of this function. It does not even work, tried to add exit() to the very beginning of the function.
Here you can make sure of this http://alexsportfolio.esy.es/web/post/index . (And in the source code you can find the line. You Необходимо авторизоваться in at the very end of the code. This is another way of adding the function)
So I also tried:
public function rules(){ return [ [['text'], 'required', 'message' => 'Заполните это поле'], ['text', 'string', 'min' => 6, 'message' => 'Слишком короткое сообщение'], ['text', 'required', 'when' => function($model){ return (Yii::$app->user->isGuest); },'message'=>'Необходимо авторизироваться'], ]; }
scenarioin which this validation will occur. Something likepublic function scenarios() { $scenarios = ['some_scenario' => ['text'], ]; return array_merge(parent::scenarios(), $scenarios);public function scenarios() { $scenarios = ['some_scenario' => ['text'], ]; return array_merge(parent::scenarios(), $scenarios);- Alexey Shimansky