Yii2: Conditional Validator always returns required
Asked Answered
R

5

14

I am trying to use Yii2's conditional validator as given in the guide like:

Model code

public function rules()
{
   // $discharged = function($model) { return $this->discharged == 1; };
    return [
        [[ 'admission_date','discharge_date', 'doctor_appointment_date', 'operation_date'], 'safe'],
        [[ 'package','tpa_name', 'discharged', 'bed_type', 'room_category', 'ref_doctor', 'consultant_doctor', 'operation_name'], 'integer'],
        [['advance_amount', 'operation_charges'], 'number'],
        [['patient_name', 'ref_doctor_other'], 'string', 'max' => 50],
        [['general_regn_no', 'ipd_patient_id'], 'string', 'max' => 20],
        [['admission_date', 'discharge_date', 'doctor_appointment_date', 'operation_date'],'default','value'=>null],
        ['ipd_patient_id', 'unique'],

        [['bed_type','admission_date','room_category'],'required'],

        ['discharge_date', 'required', 'when' => function($model) {
            return $model->discharged == 1;
        }],


    ];
}

and in my Controller like:

public function actionCreate()
    {
        $model = new PatientDetail();     

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

But whether I select or not the discharged field which is a checkbox field, the discharge date alwasys returns as required.

What I am doing wrong here?

Russia answered 27/2, 2015 at 2:13 Comment(7)
Add the rest validation rules, scenarios (if they are exist) and controller code.Mcferren
Hi arogachev - there is no hint in the docs for controller code for this validation, can you elaborate how to do that?Russia
I mean post what you have at this moment.Mcferren
OK I have updated the question with the view code for these two fields. Do you need any other info.Russia
Actually I asked about model and controller code (see first comment).Mcferren
Sorry arogachev - updated the model and controller code.Russia
@Russia do you get any solution? i am facing same issue.Dianthus
T
42

It seems that by default Yii2 will do validation in BOTH Server side and Client side. Check the example in Conditional Validation part of Yii2 doc:

['state', 'required', 'when' => function ($model) {
    return $model->country == 'USA';
}, 'whenClient' => "function (attribute, value) {
    return $('#country').val() == 'USA';
}"],

you also need a 'whenClient' code, or as @Alexandr Bordun said, to disable the client validation by 'enableClientValidation' => false.

Troika answered 23/5, 2015 at 23:13 Comment(0)
S
16

Try to add enableClientValidation parameter as the following:

 ['discharge_date', 'required', 'when' => function($model) {
        return $model->discharged == 1;
 }, 'enableClientValidation' => false]
Shinny answered 9/3, 2015 at 1:55 Comment(0)
A
4

This only worked for me when using the model name (client validation).

['state', 'required', 'when' => function ($model) {
  return $model->country == 'USA';
}, 'whenClient' => "function (attribute, value) {
  return $('#MODELNAMEHERE-country').val() == 'USA';
}"]
Aggravate answered 8/11, 2016 at 11:37 Comment(0)
S
4
['package_id_fk', 'required', 'when' => function($model) {return $model->type == 'PACKAGE';}, 'enableClientValidation' => false],

It works for me.

Synchronous answered 28/2, 2018 at 7:33 Comment(0)
A
1

i had the similar requirement i solved it using the following code

['discharge_date', 'required', 'whenClient' => function($model) {
        return $model->discharged == 1;
 }, 'enableClientValidation' => false]
Artie answered 7/12, 2015 at 5:47 Comment(1)
same answer as @Alexandr Bordun but with a type: 'whenClient' should be 'when'. You can't use a PHP function on the client side ;)Proportional

© 2022 - 2024 — McMap. All rights reserved.