Client-side validation for "either one is required" rule

Hello.

I’m working on a pretty simple contact form, where user should specify either email or phone number (or both). I used conditional validation and it works for the most part (both server side and client side). The problem is, during the client side validation, if user clicks on both fields (phone and email), they will both display the error message (something like “You need to enter either phone number or email”). Now, if user fills one of the fields, the other one will still be displaying error message, even though it actually validates correctly if user submits form. Here is my validation rule:




[['email', 'phone'], 'required', 'when' => function($model) {

                return $model->email == null && $model->phone == null;

            }, 'whenClient' => "function (attribute, value) {

                return $('#contactform-email').val() == '' && $('#contactform-phone').val() == '';

            }", 'message' => 'You need to enter either phone number or email'],



So, maybe i should try a different approach? Or is there a way to trigger client-side validation of a field by changing another field value?

Sorry for my bad english. Thank you and best regards.

Hi i checked your concept. It is working fine.

Note: if you given the ‘required’ to email like below it will show the error.




[['name', 'email', 'subject', 'body'], 'required'],// remove email from here

[['email', 'phone'], 'required', 'when' => function($model) {

               return $model->email == null && $model->phone == null;

            }, 'whenClient' => "function (attribute, value) {

                return $('#contactform-email').val() == '' && $('#contactform-phone').val() == '';

            }", 'message' => 'You need to enter either phone number or email'],