For example, I want when “both the wrong number and the limit is 10 people” - different validation error messages appear; since one appears in Russian, and the second in English.

About what I want:

[ ['peop_number'], 'integer', 'message' => 'Должно быть числом', 'max' => 10, 'message'=>'Не может превышать 10 человек', ], 

    3 answers 3

    message is a general validator error message.


    Many validators, in addition to the message , have additional error messages that describe the error more accurately. In particular, the integer validator has a tooBig property that describes an error that occurred in the case of an invalid value of the max property.


    Your rules should look like this:

     public function rules() { return [ [ 'peop_number', 'integer', 'message' => 'Должно быть числом', 'max' => 10, 'tooBig' => 'Не может превышать {max} человек', ], ]; } 

    Where, {max} is “placeholder”, which will be automatically replaced with the value of the max property.

      Try this:

       public function rules() { return [ ['peop_number', 'integer', 'message'=>'Должно быть числом'], ['peop_number', 'validateMaxValue'], ]; } public function validateMaxValue() { if ($this->peop_number > 10) { $this->addError('peop_number', 'Не может превышать 10 человек'); } } 
      • Working option, thank you. But below, a more accurate and concise way, I think, and more standard. - dima buhayov
      • 2
        @dimabuhayov, yeah, another option in this situation will be more correct, but this one will also come in handy in case of non-standard validation :) - MasterAlex

      When dumping the model, the error message keys can be viewed:

       object(yii\validators\StringValidator)#177 (20) { ... ["message"]=> string(70) "Значение «{attribute}» должно быть строкой." ["tooShort"]=> NULL ["tooLong"]=> NULL ["notEqual"]=> string(183) "Значение «{attribute}» должно содержать {length, number} {length, plural, one{символ} few{символа} many{символов} other{символа}}." ... }