Tôi cố gắng để sử dụng validator có điều kiện Yii2 của như được đưa ra trong hướng dẫn như:Yii2: Conditional Validator luôn trả về yêu cầu
đang Mẫu
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;
}],
];
}
và trong điều khiển của tôi như:
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,
]);
}
}
Nhưng liệu tôi có chọn hay không trường đã thoát là trường hộp kiểm, thì ngày tháng xuất hiện trả về theo yêu cầu.
Tôi đang làm gì sai ở đây?
Thêm các quy tắc xác thực còn lại, các tình huống (nếu chúng tồn tại) và mã điều khiển. – arogachev
Hi arogachev - không có gợi ý nào trong tài liệu cho mã điều khiển để xác thực này, bạn có thể giải thích cách thực hiện điều đó không? – Pawan
Tôi muốn đăng những gì bạn có vào lúc này. – arogachev