我正在为CodeIgniter中的验证设置回调函数,但是出现错误,显示“无法访问与您的字段名称对应的错误消息”。希望有人能帮我解决问题。
这是我的控制器:
<?php class User_Registration extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('UserRegistration');
}
public function index()
{
$this->load->helper('form');
$this->load->library('form_validation');
$data['results'] = $this->UserRegistration->get_record();
$this->load->view('templates/header');
$this->load->view('User_Registration', $data);
$this->load->view('templates/footer');
}
public function register()
{
$this->load->helper('form');
$this->load->library('form_validation');
$this->form_validation->set_rules('Ins_ID', 'Ins_ID', 'trim|required|xss_clean|callback_check_Ins_ID');
$this->form_validation->set_rules('Ins_Name', 'Ins_Name', 'required');
$this->form_validation->set_rules('Gender', 'Gender', 'required');
$this->form_validation->set_rules('Address', 'Address', 'required');
$this->form_validation->set_rules('Contact_No', 'Contact_No', 'required');
$this->form_validation->set_rules('Email', 'Email', 'required');
$this->form_validation->set_rules('Education_Level', 'Education_Level', 'required');
$this->form_validation->set_rules('Commence_Date', 'Commence_Date', 'required');
$this->form_validation->set_rules('User_Name', 'User_Name', 'trim|required|xss_clean|callback_check_User_Name');
$this->form_validation->set_rules('Password', 'Password', 'required');
if ($this->form_validation->run() === FALSE) {
$data['results'] = $this->UserRegistration->get_record();
$this->load->view('templates/header');
$this->load->view('User_Registration', $data);
$this->load->view('templates/footer');
} else {
$my_action = $this->input->post('submit');
if ($my_action == 'submit') {
$this->UserRegistration->insert_record();
}
$data['results'] = $this->UserRegistration->get_record();
$this->load->view('templates/header');
$this->load->view('Registration_Done');
$this->load->view('templates/footer');
}
}
function check_Ins_ID($Ins_ID)
{
//Field validation succeeded. Validate against database
$Ins_ID = $this->input->post('Ins_ID');
$result = $this->UserRegistration->check_ins_id($Ins_ID);
//query the database
if ($result) {
return TRUE;
} else {
$this->form_validation->set_message('check_Ins_ID', 'Invalid Instructor ID.Please contact to the administration.');
return false;
}
}
function check_User_Name($User_Name)
{
//Field validation succeeded. Validate against database
$User_Name = $this->input->post('User_Name');
$result = $this->UserRegistration->check_user_name($User_Name);
//query the
database
if ($result) {
$this->form_validation->set_message('check_User_Name', 'The User_Name already exist.Please use other User_Name.Thank you.');
return TRUE;
} else {
return FALSE;
}
}
}
?>
这是我的模特:
function check_ins_id($Ins_ID)
{
$this -> db -> select('Ins_ID');
$this -> db -> from('user');
$this -> db -> where('Ins_ID',$Ins_ID);
$this -> db -> limit(1);
$query = $this -> db -> get();
if ($query -> num_rows() == 1) {
return $query->result();
} else {
return false;
}
}
function check_user_name($User_Name)
{
$this -> db -> select('User_Name');
$this -> db -> from('login');
$this -> db -> where('User_Name',$User_Name);
$this -> db -> limit(1);
$query = $this -> db -> get();
if ($query -> num_rows() == 1) {
return $query->result();
} else {
return false;
}
}
答案 0 :(得分:0)
看看是否有帮助:
function check_Ins_ID($Ins_ID)
{
//Field validation succeeded. Validate against database
//$Ins_ID = $this->input->post('Ins_ID'); //you are already getting this in your parameters
$result = $this->UserRegistration->check_ins_id($Ins_ID);//query the database
if($result)
{
return TRUE;
}
else
{
$this->form_validation->set_message('check_Ins_ID', 'Invalid Instructor ID.Please contact to the administration.');
return false;
}
}
当您调用验证函数时,您将获得回调函数参数中的字段值,而不是来自$_POST
。