简体   繁体   English

Yii2:条件验证器始终返回所需

[英]Yii2: Conditional Validator always returns required

I am trying to use Yii2's conditional validator as given in the guide like: 我正在尝试使用指南中给出的Yii2的条件验证器,如:

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. 但是,无论我是否选择了作为复选框字段的放电字段,放电日期alwasys都会根据需要返回。

What I am doing wrong here? 我在这做错了什么?

It seems that by default Yii2 will do validation in BOTH Server side and Client side. 似乎默认情况下Yii2将在BOTH服务器端和客户端进行验证。 Check the example in Conditional Validation part of Yii2 doc : 检查Yii2 doc的 Conditional Validation部分中的示例

['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 . 你还需要一个'whenClient'代码,或者像@Alexandr Bordun所说,通过'enableClientValidation' => false来禁用客户端验证。

Try to add enableClientValidation parameter as the following: 尝试添加enableClientValidation参数,如下所示:

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

这个对我有用。

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

i had the similar requirement i solved it using the following code 我有类似的要求我使用以下代码解决了它

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM