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'=>'Необходимо авторизироваться'], ]; } 
  • one
    It may be necessary to describe the scenario in which this validation will occur. Something like public 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
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

3 answers 3

Example of inline validation from the Yii2 framework documentation :

 use yii\base\Model; class MyForm extends Model { public $country; public $token; public function rules() { return [ // an inline validator defined as the model method validateCountry() ['country', 'validateCountry'], // an inline validator defined as an anonymous function ['token', function ($attribute, $params) { if (!ctype_alnum($this->$attribute)) { $this->addError($attribute, 'The token must contain letters or digits.'); } }], ]; } public function validateCountry($attribute, $params) { if (!in_array($this->$attribute, ['USA', 'Web'])) { $this->addError($attribute, 'The country must be either "USA" or "Web".'); } } } 

Your code is correct.

You need to check:

  • The presence of a text field in the current script, if scenarios declared
  • The absence of redefining the rules array is a possible successor to the model or code.

If validation doesn't work anyway, then take this example and add your functionality to it, until you face the fact that validation will stop working. So you will understand where the error is.

    You must call the validate () function on the server. For example in the controller)

    • Try to write more detailed answers. Explain what is the basis of your statement? - Nicolas Chabanovsky

    If a non-standard field / widget (for example, a kartik widget) is used in the form instead of the standard field from the Yii collection, then its additional rules may no longer work.