2014-04-24 96 views
0

我正在爲CodeIgniter中的驗證設置回調函數,但是出現錯誤,顯示「無法訪問與您的字段名相對應的錯誤消息」。希望有人能幫我解決問題。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

看看是否有幫助:

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

+0

ya,回調函數沒有問題,但顯示消息「無法訪問與您的字段名相對應的錯誤消息。」此消息使數據無法插入並將其保存到數據庫。 – Carson

+0

錯誤是在哪條線上生成的? –

+0

如果我刪除第二個回調函數「callback_check_User_Name」。該消息不出來,它的工作原理,但我需要設置User_Name的驗證。 – Carson