0

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

3 Answers 3

4

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

Sign up to request clarification or add additional context in comments.

Comments

3

Right now you are using the same validation object for both forms. All you need to do is make 2 separate objects with different names and they will behave independently of each other.

// The empty array is there because 2nd param is for passing data
$this->load->library('form_validation', array(), 'login_form');
$this->login_form->set_rules('email', 'Email', 'required');
$this->login_form->set_rules('password', 'Password', 'required');
if ($this->login_form->run()){
  // Process the form
}

Do the same thing for the register form just give it a different name.

3 Comments

My set_value() function is not giving me the values after using your solution
I just fixed a typo, maybe that was it? If not can you post your code somewhere?
for instance for the set_value() to display the email value. What would be the line?
0

I've had many difficulties to use the CI Validation class with multiple forms (i.e. a login form and a subscription form within the same page), but I've found a solution... Hope it will help. :coolsmile:

Here how to proceed : Before you define the validation rules, test the posted submit button or other hidden inputs which can define what form has been posted to your controller. Then you can define validation rules depending on each form you can be posted.

follow this - https://github.com/EllisLab/CodeIgniter/wiki/Validation-and-multiple-forms

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.