There is the usual model for the feedback form.

Fields: name, phone, email, body

Is it possible to make a check of the fields by the condition: The phone number OR the mail field must be filled.

Those. one of them is required and correct. By means of Yii2 itself without its js.

As I understand it - the scripts are a bit different. rules () also can't figure out how to apply for my case

Thank!

    1 answer 1

    You can use when and whenClient , but if both fields are empty, then both fields will be highlighted as required:

     public function rules() { ['field_1', 'required', 'when' => function ($model) { return $model->field_2 == ''; }, 'whenClient' => 'function (attribute, value) { return $("#field_2").val() == ""; }', 'message' => 'Заполните field 1 либо field 2'], ['field_2', 'required', 'when' => function ($model) { return $model->field_1 == ''; }, 'whenClient' => 'function (attribute, value) { return $("#field_1").val() == ""; }', 'message' => 'Заполните field 1 либо field 2'] }