Welcome Guest, Not a member yet? Register   Sign In
Codeigniter login form not showing errors
#1

[eluser]Unknown[/eluser]
I'm new to Codegniter so go easy on me. I'm building a simple login form and I have successfully redirected to a page when the login credentials are correct. However, if I submit an empty form I get no error messages. I'm also using set_value in the form field and codeigniter does not refill what the user inputted once the form is submitted. Somehow that data is being cleared. Here are a few things i've done for clarity sake.

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

class Account extends CI_Controller {

  public function __construct()
  {
    parent::__construct();
    $this->load->model('User_model', 'user');
  }

  public function index() {
    $this->login();
  }

  public function validate() {

    $this->form_validation->set_rules('username', 'username', 'xss_clean|required');
    $this->form_validation->set_rules('password', 'password', 'xss_clean|required|md5');

    $user_data = array(
      'username' => $this->input->post('username'),
      'password' => MD5($this->input->post('password'))
    );

    if ($this->form_validation->run() == FALSE)
    {
      $data['title'] = "Login Fool";
      $this->load->view('templates/header', $data);
      $this->load->view('login-test', $data);
      $this->load->view('templates/footer');
    }
    else
    {
      //ADD USER
      $validated = $this->user->validate($user_data['username'], $user_data['password']);
      if($validated){
        redirect(base_url() . 'account');
      }
      else{
        $this->session->set_flashdata('LoginError', 'Sorry, your credentials are incorrect.');
        redirect(base_url() . 'account/login');
      }

    }

  }

  public function login() {

      $data['title'] = "Login Fool";
      $this->load->view('templates/header', $data);
      $this->load->view('login-test', $data);
      $this->load->view('templates/footer');

  }

  public function logout()
  {
    $this->user->logout();
  }

}

login-test.php (view)
Code:
<a href="/account/logout">Logout</a>
<div class="wrapper">
  <div class="row">
    <div class="large-12 columns">
      <h1>Login Now</h1>
       &lt;?php
         if(validation_errors() != false) {
           echo "<div id='errors'>" . validation_errors() . "</div>" ;
         }
       ?&gt;

        &lt;?= ($this->session->flashdata('LoginError')) ? '<p class="LoginError">' . $this->session->flashdata('LoginError') . '</p>' : NULL ?&gt;
        &lt;?php echo form_open('account/validate'); ?&gt;
          <label for="username">Username:</label>
          &lt;input type="text" size="20" id="username" name="username" value="&lt;?php echo set_value('username'); ?&gt;"/&gt;
          <br/>
          <label for="password">Password:</label>
          &lt;input type="password" size="20" id="passowrd" name="password" value="&lt;?php echo set_value('password'); ?&gt;"/&gt;
          <br/>
          &lt;input type="submit" value="Login"/&gt;
        &lt;/form&gt;
        <br /><br />
    </div>
  </div>
</div>

Any idea why the errors won't show?
#2

[eluser]Unknown[/eluser]
Turns out my model was extending the wrong class. It was extending CI_Controller instead of CI_Model.




Theme © iAndrew 2016 - Forum software by © MyBB