Codeigniter form validation Callback with multiple field validation

I have this controller configured to log in:

<?php
class Login extends Controller {

    function __construct() {
        parent::Controller();
        $this->form_validation->set_error_delimiters('', '');
        $this->output->enable_profiler(TRUE);
    }

    function index(){

        redirect('/login/terminal');

    }

    function terminal() {
    // terminal login

        $this->form_validation->set_rules(array('username','password'), 'Username', 'callback_terminal_login_check[$username,$password]');
        if ($this->form_validation->run() == FALSE) {
            $this->load->view('login_header');
            $this->load->view('login_terminal');
            $data['version'] = $this->master->GetVersion();
            $this->load->view('login_footer', $data);
        } else {
            redirect('/terminal');
        }

    }

    function terminal_login_check($username,$password) {
    // callback function to perform terminal login  

        if ($this->authentication->DoTerminalAuthentication($username,$password)) {
            echo $username;
            return TRUE;
        } else {
            $this->form_validation->set_message('terminal_login_check', 'Invalid');
            return FALSE;
        }


    }

}

What I'm looking for is a string that does a form validation callback -> $this->form_validation->set_rules(array('username','password'), 'Username', 'callback_terminal_login_check[$username,$password]');

I know this is wrong. Basically I want to check the username and password on the Authentication-> DoTerminalAuthentication model to handle user login. I want to pass form fields $usernameand $password. Here is my form view if it helps:

<div id="title">Terminal Login</div>
<?php 
    if (validation_errors()) {
        echo '<div id="error">' . validation_errors() . '</div>';
    }
?>

<?=form_open('login/terminal');?>
<?=form_label('Username', 'username')?><br />
<?=form_input(array('id'=>'username','name'=>'username','value'=>set_value('username')))?><br />
<?=form_label('Password', 'password')?><br />
<?=form_password(array('id'=>'password','name'=>'password'))?><br />
<?=form_submit(array('name'=>'passwordsubmit','value'=>'Login >>'))?><br />
<?=form_close();?>
+5
source share
3 answers

, . , , (, ), , POST. , - .

+9

, .

$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required|callback_terminal_login_check');

- . , .

function terminal_login_check() 
{
  $username = $this->input->post('username');
  $password = $this->input->post('password');

  // LOAD MODEL HERE

  if ($this->authentication->DoTerminalAuthentication($username, $password)) 
  {
    echo $username;
    return TRUE;
  } 
  else 
  {
    $this->form_validation->set_message('terminal_login_check', 'Invalid');
    return FALSE;
  }
}

2013-07-09: , , - .

+7

I prefer to use form validation to simply validate input, for example, to check for empty fields, invalid email addresses, passwords that are too short, etc. I tend to not use form validation for more complex logic such as authentication. I would put authentication in the action method and not in the verification callback function. He would overcome your specific problem, step by step.

function terminal() 
{
    $this->form_validation->set_rules('username', 'Username', 'required');
    $this->form_validation->set_rules('password', 'password', 'required');   

    if ($this->form_validation->run()) 
    {
        $username = $this->input->post('username');
        $password = $this->input->post('password');

        if ($this->authentication->DoTerminalAuthentication($username, $password))
        {
            // Handle successful login
        }
        else
        {
            // Authentication failed. Alert the view.
            $this->load->view('terminal', array('login_failed' => true));
        }
    } 
    else 
    {
        $this->load->view('terminal', array('login_failed' => false));
    }
}

And then in the view you can place this below your login form

<?php if ($login_failed) : ?>
    <span id="login-failed-message">Login failed</span>
<?php endif; ?>
+2
source

All Articles