Answer the question
In order to leave comments, you need to log in
Yii2. Form how to ignore validation rules for hiding fields?
There is a form with two checkbox rent and sale.
when choosing rent, the input for choosing the rental period is shown
when choosing sale, this field is hidden
This input has a validation rule required
Question: how to make sure that when the checkbox sale is selected (and the input field is hidden), the specified validation rule is not taken into account on submit, but when rent is selected, it is taken into account?
Answer the question
In order to leave comments, you need to log in
In the validation rules, you can specify additional conditions like www.yiiframework.com/doc-2.0/guide-input-validatio... (Conditional Validation)
thanks i solved it this way
['period_id', 'required', 'when' => function($model) {
return $model->operation_type == Listing::OPERATION_TYPE_RENT;
}, 'whenClient' => "function (attribute, value) {
return $('.listing-operation-type input:checked').val() == " . Listing::OPERATION_TYPE_RENT . ";
}"],
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question