php Yii2:条件验证器总是返回必需的

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28756397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:03:07  来源:igfitidea点击:

Yii2: Conditional Validator always returns required

phpvalidationyii2

提问by Pawan

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.

但是无论我是否选择了复选框字段的出院字段,出院日期总是按要求返回。

What I am doing wrong here?

我在这里做错了什么?

回答by Andy Ding

It seems that by default Yii2 will do validation in BOTHServer side and Client side. Check the example in Conditional Validationpart of Yii2 doc:

似乎默认Yii2将做验证BOTH服务器端和客户端。检查Yii2 docConditional 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.

回答by Alexandr Bordun

Try to add enableClientValidationparameter as the following:

尝试添加enableClientValidation参数如下:

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

回答by Rakesh Kumar Srivastava

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

It works for me.

这个对我有用。

回答by rinaldo

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

回答by Bloodhound

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

我有类似的要求,我使用以下代码解决了它

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