How to cancel a field mandatory?

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 a field 'town', 'town_center', 'km_price' be required if there is a tick "to use the new rates" and click to submit the form. But if you squeeze the tick the mandatory fields still how to fix it?
July 9th 19 at 10:50
2 answers
July 9th 19 at 10:52
To use conditional validation.
through WhenClient to hang on your checkbox or input well, or what have you.
You have realized, but fits the model, try to use a variable from your checkbox as in the example on the link. like that.
$model->checkBox == true;
July 9th 19 at 10:54
As an option:
You can use ajax validation in action and validation to do this check. For example, it is possible to make different scenarios for the model.

Find more questions by tags Yii