I have two fields. It is necessary that the second field be validated on the client side depending on the choice of the first one. I need to dynamically change yii.validation.required (value, messages) for another field when choosing the first one, but is it possible at all? According to the standard, yii.validation.required (value, messages) works only for the current field (that is, the one with which the action is happening now). I would be grateful if you could tell me some other way I can dynamically change the validation of the second field depending on the first ...

@Blacknife I'd like to do something like

[['hours', 'minutes'], 'required', 'whenClient' => 'function (attribute, value) { if($('#customer-hours').val() == 0 && $('#customer-minutes').val() == 0){ //если hours == 0 и minutes == 0, то нужно как-то передать ошибку об обязательном заполнении в minutes из hours, а в hours оставить по дефолту как есть required } }', 'message' => 'Обязательно к заполнению'] 

    2 answers 2

    For simple client side validation, use whenClient .

    Suppose the first type field is an object type [1 => 'тип1', 2 => 'тип2'] + suppose it is select , and the second description field is required to be filled only if тип2 selected:

     ['description', 'required', 'when' => function ($model) { return $model->type == 2; }, 'whenClient' => 'function (attribute, value) { return $("select option:selected").val() === 2; }', 'message' => 'Описание обязательно к заполнению'] 

    This code describes both server and client validation, and says: the "description" field is mandatory to fill in if type 2 is selected

    There is an even more subtle clientValidateAttribute method, you need to create your own validator class for it, but based on the question I cannot say what exactly you need.

    Based on the amendments in the question:

     [['hours', 'minutes'], 'required'], ['hours', 'integer', 'min' => 0, 'max' => 23], ['minutes', 'integer', 'min' => 0, 'max' => 59], ['minutes', 'compare', 'compareValue' => 0, 'operator' => '>', 'when' => function ($model) { return $model->hours == 0; }, 'whenClient' => 'function (attribute, value) { return $('#customer-hours').val() == 0; }', 'message' => 'minutes не может быть 0 в данном случае'] 
    • I wrote to you in the main post. - RSalnikov
    • @RSalnikov try to describe more clearly this если hours == 0 и minutes == 0, то нужно как-то передать ошибку об обязательном заполнении в minutes из hours, а в hours оставить по дефолту как есть required - Blacknife
    • there is some condition where, for the values ​​of hours = 0 and minutes = 0, show the error for the minutes value that it cannot be 0 in this situation (in other cases these two fields will simply be the way it is). the problem is that if for example we have hours = 12 and minutes = 0 and we do hours = 0, then we need to somehow tell the validator on the client side that minutes cannot be 0 in this case. that's the whole problem - RSalnikov
    • @RSalnikov that is, minutes should not be 0 if hours = 0 ? - Blacknife
    • those. if minutes equals 0 and at the same time = 0, then show the validator error that minutes cannot be 0 in this case - RSalnikov

    Thanks to the help @Blacknife solved the problem on the client side as follows

      ['hours', 'required', 'whenClient' => "function(attribute){ $(attribute.input).closest('form').yiiActiveForm('validateAttribute', 'customer-minutes'); }"], ['minutes', 'compare', 'compareValue' => 0, 'operator' => '!=', 'when' => function ($model) { return $model->hours == 0; }, 'whenClient' => "function (attribute, value) { return $('#customer-hours').val() == 0; }",]