2016-03-21 26 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(); ....