如果我考虑这样的条件,在codeigniter表单验证库中是否有一种方法可以确定人员数据的重复条目: 名字,中间名,姓氏,出生日期在数据库中匹配。
例如,如果数据与被认为相同的数据库匹配,我需要检查此人是否已存在:名字,中间名,姓氏和出生日期。
在这种情况下,我无法使用is_unique
每种输入类型,因为如果此人具有相同的名字,姓氏和中间名,但生日数不同,该怎么办?如何在codeigniter中进行这样的表单验证?。
像这样的查询:
SELECT COUNT(patient_id) AS patient_id FROM patients WHERE patients.firstname = "" AND patients.middlename = "" AND patients.lastname = "" AND patients.birthdate = ""
如果查询结果大于1,则将其视为重复条目。我怎样才能在codeigniter表格验证中做到这一点?
这是我在Controller中的代码:
public function addpatient() {
$this->form_validation->set_rules('pafname', 'pafname', 'trim|required',
array('required' => 'First name is required')
);
$this->form_validation->set_rules('pamname', 'pamname', 'trim|required',
array('required' => 'Middle name is required')
);
$this->form_validation->set_rules('palname', 'palname', 'trim|required',
array('required' => 'Last name is required')
);
$this->form_validation->set_rules('paaddress', 'paaddress', 'trim|required',
array('required' => 'Address is required')
);
$this->form_validation->set_rules('pacontact', 'pacontact', 'trim|required',
array('required' => 'Contact # is required')
);
$this->form_validation->set_rules('pabdate', 'pabdate', 'trim|required',
array('required' => 'Birthdate is required')
);
$this->form_validation->set_rules('psex', 'psex', 'trim|required',
array('required' => 'Gender is required')
);
$this->form_validation->set_rules('pmartialstat', 'pmartialstat', 'trim|required',
array('required' => 'Martial status is required')
);
if ($this->form_validation->run() == TRUE) {
$result['status'] = true;
$result['message'] = "This patient has no duplicate";
}else {
$result['status'] = false;
$result['message'] = $this->form_validation->error_array();
}
echo json_encode($result);
}
答案 0 :(得分:0)
创建自己的custom validation method。在自定义验证方法中,调用SQL以查看是否存在所有字段匹配的记录,并根据匹配返回TRUE / FALSE。
类似的东西:
$this->form_validation->set_rules('pafname', 'pafname', 'trim|required|callback__duplicate_validation',
array('required' => 'First name is required',
'callback__duplicate_validation' => 'Duplicate record')
);
function _duplicate_validation($pafname) {
// Get _POST data for all fields since only pafname is passed in
// Call SQL to see if duplicate
// if duplicate, return FALSE
// else return TRUE
}