Welcome Guest, Not a member yet? Register   Sign In
How to run validation and controller logic in one function?
#1

[eluser]RaGe10940[/eluser]
So I am trying to be as "lazy" as possible when it comes to the amount of code I can write. Usually my controller would look like so :

Code:
public function index() {// Load login page
            $data['content'] = 'homepage/login';
            $this -> load -> view('templates/no_jsTemplate', $data);
            $this -> session -> unset_userdata('logged_in');
    }

that is to load the view

and then another function to accept the form like so :


Code:
public function login_user() {
     $this -> load -> library('form_validation');
     if ($this -> form_validation -> run('c_homepage/login_user') == FALSE) {// If validation fails
         $this -> index();
         // Send to login page
     } else {
         $this -> load -> model('m_homepage');
         // Load the model
          $value = $this -> input -> post();
          // Assign $_POST to $value
          if ($this -> m_homepage -> login_user($value)) {// Runs bcrypt and verifies the users -> updates last login
                  redirect('c_dashboard/index');
          } else {
                $this -> index($message = 'Invalid Login Attempt');
          }
     }
}


Now what I am trying to do is condense this into one method but I keep getting stuck in a loop after my validation error returns FALSE. does any one have any work arounds for this?

Code:
public function index() {// Load login page
        if ($this -> input -> post('login_submit') == 'Login') {
            $this -> load -> library('form_validation');
            if ($this -> form_validation -> run('c_homepage/login_user') == FALSE) {// If validation fails
                $this -> index();
                // Send to login page
            } else {
                $this -> load -> model('m_homepage');
                // Load the model
                $value = $this -> input -> post();
                // Assign $_POST to $value
                if ($this -> m_homepage -> login_user($value)) {// Runs bcrypt and verifies the users -> updates last login
                    redirect('c_dashboard/index', 'refresh');
                } else {
                    $this -> session -> set_flashdata('error', 'Invalid Login Attempt - Retry');
                    redirect('c_homepage/index', 'location');
                }
            }
        } else {
            $data['content'] = 'homepage/login';
            $this -> load -> view('templates/no_jsTemplate', $data);
            $this -> session -> unset_userdata('logged_in');
        }
    }


Messages In This Thread
How to run validation and controller logic in one function? - by El Forum - 08-23-2013, 05:37 PM



Theme © iAndrew 2016 - Forum software by © MyBB