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');
        }
    }
#2

[eluser]jairoh_[/eluser]
after loading the library, set some rules like
Code:
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
then run, the run method does not have any params
Code:
if ( $this->form_validation->run() == FALSE ) {
      //do something
} else {
      //do something
}
hope this contributes
#3

[eluser]M Arfan[/eluser]
Code:
public function login() {
      
            $this -> load -> library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
if ( $this->form_validation->run() == FALSE ) {
      //do something
} else {
      //do something
}  

}

that should help you




Theme © iAndrew 2016 - Forum software by © MyBB