@deron
To validate a specific field in Yii2, you can follow these steps:
1 2 3 4 5 6 7 |
public function rules() { return [ // other rules [['email'], 'email'], ]; } |
1 2 3 4 5 6 7 8 |
public function rules() { return [ // other rules [['email'], 'email'], [['email'], 'safe'], ]; } |
1 2 3 4 5 6 7 8 |
$model = new YourModel; $model->email = $emailValue; // Set the value of the email field if ($model->validate(['email'])) { // Field is valid } else { // Field is invalid $errors = $model->getErrors('email'); // Get the validation errors for the specific field } |
By specifying the field as an argument to the validate()
method, you can ensure that only the validation rules for that field are applied. This allows you to validate specific fields without having to validate the entire model.