I have 2 forms in one page which are register and login forms and i define a name for each form so i can post it seperately, but how can i ONLY write 1 form_validation-run() ?
public function index()
{
//load form_validation and session library
$this->load->library(array('form_validation','session'));
if ( $this->input->post('register') ) {
$this->form_validation->set_rules('first_name', 'First name', 'required');
$this->form_validation->set_rules('last_name', 'Last name', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
$this->form_validation->set_rules('password', 'Password', 'required|min_length[6]');
if ( $this->form_validation->run() !== FALSE ){
// to create an account
} else {
$this->session->set_flashdata('msg', validation_errors('<div>','</div>'));
redirect('/','location');
}
} elseif ( $this->input->post('login')) {
$this->form_validation->set_rules('email', 'Email', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
if ( $this->form_validation->run() !== FALSE ) {
// to get login
} else {
$this->session->set_flashdata('msg', validation_errors('<div>','</div>'));
redirect('/','location');
}
}
$this->load->view('templates/header');
$this->load->view('pages/index');
$this->load->view('templates/footer');
}
Logically you need to call it twice with your code as you are branching on the button value.
That said, for tidiness, you could be sending them to separate actions (you are almost doing that anyway at the moment). login form to e.g. /users/login and then the register form to e.g. /users/create or /users/save