C
C
Carlos Rodriguez2015-01-26 12:15:34
Yii
Carlos Rodriguez, 2015-01-26 12:15:34

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

3 answer(s)
V
Vit, 2015-01-26
@asf

In the validation rules, you can specify additional conditions like www.yiiframework.com/doc-2.0/guide-input-validatio... (Conditional Validation)

C
Carlos Rodriguez, 2015-01-26
@asf

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 . ";
            }"],

B
bxN5, 2016-06-04
@bxN5

here is a good article on rules and scripts phpinfo.info/yii2-scenario-rules

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question