2012-05-16 33 views
19

我有一個回調函數check_captcha,它看到$row==0還是== 1(從sql查詢此信息)。無法訪問對應於您的字段名稱的錯誤消息

問題是,我不能從$self->form_validation->set_rule('captcha', 'call_back_check_captcha')調用它,因爲我的函數需要一個$row var。我現在調用它的方式我得到一個無法訪問錯誤消息。我該如何做這項工作?

function check_captcha($row) 
{ 
    if($row ==0)//didnt find any 
    { 
     $this->form_validation->set_message('captcha', 'text dont match captcha'); 
     return FALSE; 
    } 
    else 
    { 
     return TRUE; 
    } 
} 


function create_member() 
     { 

      $past = time() - 7200; 

     $this->db->query("DELETE FROM captcha WHERE captcha_time <".$past);     
     $sql = "SELECT COUNT(*) AS count FROM captcha WHERE word =? AND ip_address =?";  
     $binds = array($_POST['captcha'], $this->input->ip_address(), $past); 
     $query= $this->db->query($sql, $binds); 

     $row = $query->row(); //row query rows : if it found an entry =1 

      $self->check_captcha($row->count); 

     //VALIDATIONS 
     $this->form_validation->set_rules('first_name', 'First Name', 'trim|required'); 
     $this->form_validation->set_rules('last_name', 'Last Name', 'trim|required');  
     $this->form_validation->set_rules('email_address', 'Email Address', 'trim|required|valid_email|unique[user.email_address]'); 
     $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]|unique[user.username]'); 
     $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_leng[32]'); 
     $this->form_validation->set_rules('password2', 'Password Confirmation','trim|required|matches[password]'); 
     if(!$_POST['captcha']){ 
     $this->form_validation->set_rules('captcha', 'Captcha','trim|required');}else{ 
     $this->form_validation->set_rules('captcha', 'Captcha', 'callback_check_captcha');} 

     if($this->form_validation->run()==FALSE) 
     { //this -> to the curr obj(UserController) && registraion() points to the the function in controller 
      $this->registration(); //reloads reg page so they can fill out right stuff 
     } 
     else 
+0

前加上這個,你建議立即進行刪除在函數名前加上一個'_',讓我無法從URL調用。 'check_captcha'應該是'_check_captcha' –

回答

42
$this->form_validation->set_message('check_captcha', 'text dont match captcha'); 

消息名稱對應於功能,而不是字段。因此將其設置爲「check_captcha」將修復您的錯誤。錯誤消息將使用正確的字段名稱。

+0

LOL哇這樣的noobie錯誤= [thnx !!! –

+1

做了同樣的錯誤thnx –

8

實際上,最好的方法是,不要直接在控制器上寫錯誤消息,而是在語言中添加這個條目「check_captcha」。

在我的情況下,驗證規則(表單驗證)「less_than」的消息不存在。

我更改了文件/system/language/??/form_validation_lang.php。我已添加缺少的條目。

+0

是的。我還通過添加缺少的語言文本來解決此消息。 – mytharcher

3

,幫助我

去的application/config/autoload.php,並加上 「安全」 的輔助類有。

$autoload['helper'] = array('security'); 

或者你的表單驗證

$this->load->helper('security'); 
+1

nie trick..it works! –

+0

我很高興我的幫助 –

相關問題