Welcome Guest, Not a member yet? Register   Sign In
Throwing a validation error when the username and password is incorrect
#1

[eluser]merik[/eluser]
I am using form validation to show warning messages to the user when the username and password doesn't meet the required validation rules:

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Adminlogin extends CI_Controller {

function __construct()
{
  parent::__construct();
  session_start();
}

public function index()
{
  $this->load->library('form_validation');
  $this->form_validation->set_rules('username', 'Username',
   'required|validemail');
  $this->form_validation->set_rules('password', 'Password',
   'required|min_length[4]');

  if ($this->form_validation->run() !== false) {
   $this->load->model('adminauth');
   $auth = $this->adminauth->authenticate(
    $this->input->post('username'),
    $this->input->post('password'));
   if ($auth !== false) {
    $_SESSION['username'] = $this->input->post('username');
    redirect('admin');
   } else {
    # ...
   }  
  }

  $this->load->view('login');
}
}

This way, if the username is missing or password is too short, I show a warning message above the form:
Code:
&lt;?php echo validation_errors('<div class="alert alert-error">','</div>'); ?&gt;

I was wondering if I could also throw a validation error when the username and password passes the rules, but don't match any record in the database. I can do it manually (by creating another DIV like the last one, and showing a message in it, when authentication fails), but I wonder if I can reuse the validation_errors() function for that.

I thought of using callback functions, but they are only for single input fields, not a combination of username and password. Any idea?
#2

[eluser]Aken[/eluser]
You can use $this->input->post() in your callback.
#3

[eluser]merik[/eluser]
[quote author="Aken" date="1359795096"]You can use $this->input->post() in your callback.[/quote]
Can you be more specific please? Or give an example. I'm not sure if I can understand your recommendation.
#4

[eluser]Aken[/eluser]
You can use that method to retrieve POST values of other fields, not just the one sent to your callback.




Theme © iAndrew 2016 - Forum software by © MyBB