2016-03-21 23 views
0

我發現其他人遇到同樣的問題,但無法找到解決問題的方法。如果使用回調函數登錄失敗,則不會顯示我的錯誤消息。未顯示CodeIgniter回調函數

loginController.php

public function validate_credentials() { 

     $this->load->model('users_model'); 
     $query = $this->users_model->validate(); 

     if($query) 
     { 
     $data = array(
      'username' => $this->input->post('username'), 
      'is_logged_in' => true 
     ); 
     $this->session->set_userdata($data); 
     redirect('/member/homes'); 
     } 
     else 
     { 
      $this->form_validation->set_rules('username', 'Username', 'trim|required|callback_check_username_exists'); //test 
     $data['lofin_failed'] = 'Wrong usename or password'; // I added this line in the meantime to display an error message. 

      $this->load->view('header'); 
      $this->load->view('navbar'); 
      $this->load->view('loginForm', $data); 
      $this->load->view('footer');    
     } 
    } 

    public function check_username_exists() 
    { 
     $this->form_validation->set_message('check_username_exists', 'Wrong username or password!!'); 
     return false; 
    } 

users_model.php

public function validate() {   
     $this->db->where('username', $this->input->post('username')); 
     $this->db->where('password', md5($this->input->post('password'))); 
     $query = $this->db->get('game_players'); 

     if($query -> num_rows() == 1) {  // if we have one match 
      return true; 
     }  
    } 
+0

您的回調函數應該有一個參數。嘗試將它聲明爲'check_user_exists(username)' –

+0

*'公共函數check_username_exists($ username)' –

+0

我在函數declarion中添加了'$ username'但它沒有幫助。有沒有辦法看到郵件的內容或爲什麼不顯示? – remyremy

回答

0

有形式驗證規則:

is_unique [table.field]

檢查數據庫,如果用戶名是唯一的,你不需要回調。

您可以點擊此處查看https://ellislab.com/codeigniter/user-guide/libraries/form_validation.html

而且你可以使用會話flashdata而不是$數據[「lofin_failed」] 您可以檢查這裏https://ellislab.com/codeigniter/user-guide/libraries/sessions.html

+0

很高興知道is_unique,但是在這裏我需要檢查用戶名是否與數據庫中的密碼相匹配,以至於不足以檢查用戶名是否唯一 – remyremy

+0

因此,如果用戶不在用戶中,您希望使用回調來顯示驗證錯誤你的數據庫?如果我是正確的,你爲什麼不從模型返回num_rows,然後檢查控制器,如果它是0,如果是,請設置閃存數據並將其重定向到主頁,在那裏你有div來檢查flashdata錯誤或類似的東西。 '$ this-> session-> set_flashdata('error','用戶名或密碼不正確。');重定向('/ home','refresh');'在主頁上您將擁有'<?php if ($ this-> session-> flashdata('error')!='')echo「

".$this->session->flashdata('error')."
」; ?>' – kunicmarko20

0

我發現這個問題。我只是忘了在頁面的開頭添加echo validation_errors(); ....