CodeIgniter: Not showing login error message
Could you please help me work out why my code will not show the setmessage:
$this->form_validation->set_message('Sorry %s is not correct.');
The validation is happily showing that they are required:
home.php -> controller
<?php
ob_start();
if ( ! defined('BASEPATH')) exit('No direct开发者_高级运维 script access allowed');
class Home extends CI_Controller {
function __construct(){
parent::__construct();
}
function index(){
$this->login();
}
public function login()
{
//Loads The Page Template
$this->template->set('title','Admin Login');
//Validation Check
$this->form_validation->set_rules('username','Username','required|trim|max_length[50]|xss_clean');
$this->form_validation->set_rules('password','Password','required|trim|max_length[200]|xss_clean|callback_checkUsernamePassword');
if($this->form_validation->run() == FALSE){
$this->template->load('template','admin/admin_login');
}else{
extract($_POST); // Gets data from form and creates vars
$user_id = $this->login_model->check_login($username,$password);
if(! $user_id || $password){ // != If username or password are not correct
$this->session->set_flashdata('login_error',TRUE); //does not add the non valid login to the session
$this->form_validation->set_message('Sorry %s is not correct.');
redirect('admin');
}else{
$this->session->set_userdata('logged_in',TRUE);
$this->session->set_userdata('user_id',$user_id);
redirect('admin/dashboard');
}
}
}
function logout(){
$this->session->unset_userdata('logged_in');
echo 'You have now been logged out';
redirect('admin');
}
}
//End of file home.php
//Location: ./application/controllers/admin/home.php
login_model.php -> model
<?php
class Login_model extends CI_Model
{
function __construct()
{
parent::__construct();
}
function Login_model(){
parent::Model();
}
function check_login($username,$password){
$MD5_password = md5($password); // Tells the db that the password is a MD5 #
$query_str ="SELECT id FROM users WHERE email = ? and password = ?"; // Tells the db that this is a query
$result = $this->db->query($query_str, array($username, $MD5_password)); // Result
//If it is all correct
if($result->num_rows() == 1){
return $result->row(0)->id;
}else{
return false;
}
}
}
?>
I have tried the following:
$lang['error'] = "Sorry your %s is incorrect.";
- This is set in the lang file
and
$this->form_validation->set_message('error','Sorry %s is not correct.');
- I am unsure what the 2nd para must be
Your really really really should read the user_guide. Your logic is incorrect. For example, you didn't use your callback. That's why your error messages don't show. I have put in a few comments for you to read.
public function login()
{
$this->template->set('title','Admin Login');
$this->form_validation->set_rules('username','Username', 'required|trim|max_length[50]|xss_clean');
// You aren't using the callback here.
$this->form_validation->set_rules('password','Password', 'required|trim|max_length[200]|xss_clean|callback_checkUsernamePassword');
if($this->form_validation->run() == FALSE){
$this->template->load('template','admin/admin_login');
}else{
// You shouldn't be adding messages when the validation has already passed. The setting should be when the validation is false.
extract($_POST);
$user_id = $this->login_model->check_login($username,$password);
if(! $user_id || $password){
$this->session->set_flashdata('login_error',TRUE); //does not add the non valid login to the session
$this->form_validation->set_message('Sorry %s is not correct.');
redirect('admin');
}else{
$this->session->set_userdata('logged_in',TRUE);
$this->session->set_userdata('user_id',$user_id);
redirect('admin/dashboard');
}
}
}
Here's what you should do. I'm not going to code everything but will give you an idea.
public function login()
{
$this->template->set('title','Admin Login');
$this->form_validation->set_rules('username','Username', 'required|trim|max_length[50]|xss_clean');
$this->form_validation->set_rules('password','Password', 'required|trim|max_length[200]|xss_clean|callback_checkUsernamePassword');
if($this->form_validation->run() == TRUE){
$this->session->set_userdata('logged_in',TRUE);
$this->session->set_userdata('user_id',$user_id);
redirect('admin/dashboard');
}
$this->template->load('template','admin/admin_login');
}
public function checkUsernamePassword() {
extract($_POST); // Gets data from form and creates vars
$user_id = $this->login_model->check_login($username,$password);
if(! $user_id || $password){ // != If username or password are not correct
$this->session->set_flashdata('login_error',TRUE); //does not add the non valid login to the session
$this->form_validation->set_message('checkUsernamePassword', 'Sorry %s is not correct.');
return FALSE
}else{
$this->session->set_userdata('logged_in',TRUE);
$this->session->set_userdata('user_id',$user_id);
redirect('admin/dashboard');
}
}
The set_message function for CodeIgniter's Form Validation class takes two parameters - the first being the name of the validation rule you would like to modify the message for, and the second being the modified message itself.
http://codeigniter.com/user_guide/libraries/form_validation.html
So if you would like to modify the message for the required fields, you would use:
$this->form_validation->set_message('required', 'Sorry %s is not correct.');
$this->form_validation->set_message();
This is used when you are trying to make your own Validation Functions or setting new error messages
If you are trying to make your own validation function then you should follow below article
http://codeigniter.com/user_guide/libraries/form_validation.html
What you can do is according to the returned value you can set the $this->session->set_flashdata(); and show it on the admin page.
http://codeigniter.com/user_guide/libraries/sessions.html
精彩评论