【发布时间】:2015-04-29 15:40:33
【问题描述】:
我正在尝试使用指南中给出的 Yii2 的条件验证器,例如:
型号代码
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;
}],
];
}
在我的控制器中,例如:
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,
]);
}
}
但是无论我是否选择了作为复选框字段的出院字段,出院日期总是按要求返回。
我在这里做错了什么?
【问题讨论】:
-
添加其余验证规则、场景(如果存在)和控制器代码。
-
嗨 arogachev - 文档中没有关于此验证的控制器代码的提示,您能详细说明如何做到这一点吗?
-
我的意思是发布你现在拥有的东西。
-
好的,我已经用这两个字段的视图代码更新了问题。您还需要其他信息吗?
-
其实我问的是模型和控制器代码(见第一条评论)。
标签: php validation yii2