[[+content_image]]
L
L
Leopandro2016-05-28 16:49:26
Yii
Leopandro, 2016-05-28 16:49:26

How to remove the required field?

public function rules()
    {
        return [
          [['car_id', 'driver_id'], 'required'],
            [['car_id', 'driver_id', 'use_new_tariffs', 'town', 'town_center', 'km_price'], 'integer'],
            [['car_id'], 'exist', 'skipOnError' => true, 'targetClass' => Car::className(), 'targetAttribute' => ['car_id' => 'id']],
            [['driver_id'], 'exist', 'skipOnError' => true, 'targetClass' => Driver::className(), 'targetAttribute' => ['driver_id' => 'id']],
          [['town', 'town_center', 'km_price'], 'required', 'when' => function($model){
            return $model->use_new_tariffs;
          }],
        ];
    }

I have the fields 'town', 'town_center', 'km_price' become mandatory if the "use new rates" checkbox is checked and click submit the form. But if you uncheck the box, then the mandatory fields still remain early, how can I fix it?

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
A
Anton Natarov, 2016-05-28
@HanDroid

Use conditional validation.
through WhenClient hang on your checkbox or input, well, or whatever you have.
You have it implemented, but the model is stuck, try using the variable from your checkbox as in the example at the link. like this.
$model->checkBox == true;

M
Maxim Timofeev, 2016-05-29
@webinar

As an option:
You can use ajax validation and do this check in the validation action. For example, you can make different scenarios for the model.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question