A
A
Alexey Kuznetsov2014-10-28 14:48:44
Yii
Alexey Kuznetsov, 2014-10-28 14:48:44

How to highlight multiple fields in yii2 for one validation error?

Is it possible to highlight several related fields at once while validating the active form, displaying only one error??

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stanislav Gamayunov, 2014-11-06
@happyproff

In Yii1, the logic was as follows: validation is assigned to one of the fields through a class method in which errors are set for several fields at once.

public function rules () {
    return [
        ['field1', 'customValidate'],
    ];
}

public function customValidate ($attribute, $params) {
    if (some_expression) {
        $this->addError('field1', 'Error description');
        $this->addError('field2', '');
        $this->addError('field3', '');
    }
}

In Yii2, I think you can do it a little easier:
public function rules () {
    return [
        [
            'field1',
            function($attribute, $params){
                if (some_expression) {
                    $this->addError('field1', 'Error1 description');
                    $this->addError('field2', '');
                    $this->addError('field3', '');
                }
            }
        ],
    ];
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question