Answer the question
In order to leave comments, you need to log in
How can you shorten rules in form validation in Yii2?
We have a code from a Yii2 model
<?php
namespace app\models;
use yii\base\Model;
class EntryForm extends Model
{
public $name;
public $email;
public function rules() {
return [
[['name'], 'required'],
['email', 'email']
];
}
}
Answer the question
In order to leave comments, you need to log in
Not only do you not use tags, but you also don’t read the documentation at all!
Why do you write such a footcloth ?!
Here, everything is short and clear.
The way you wrote is for validating a single field:
That's all the abbreviation for you!
And you are still validating it.
Try to set a field with type string to a numeric value. Immediately, the error will come out and the red frame will be ... Everything will be.
PS
Or in general, try to write it down in the rules, maybe it will work[$this->attributes(), 'string']
If you want to write shitty code and assign values yourself without validation, you can not use rules().
If you want to accept array values via ->attributes($post), but without validation, you need to list the allowed fields with at least one rule() line.
If you want to write smart code, you need to validate everything.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question