Sometimes it’s required to place two forms on one page in codeigniter like displaying login form and register form on same page. Although the views may be different but you can face conflict while displaying validation errors if the form actions are not separate (aren’t calling different methods in controller). Here is a workaround to validate one form at a time while placing multiple forms on one page in codeigniter.
The scenario
- I have a view having both login and register forms (You should use two different views to keep code clean).
- The form action is same for both login/register buttons.
- If any of form validation fails, I have to check which form is being submitted and have to show errors within that particular form.
If both forms are calling different actions (controller/method) then there would be no problem but if you didn’t follow the right approach then the both form will display the same error messages whenever you submit either of them as they are handled by the same controller method.
The solution
Before running validation, check for the existence of either a hidden field, a field that is unique to the form, or you can check the value of the particular submit button. Let us see how can implement the concept.
Multiple forms on one page / controller and form validation
According to the solution specified above you can modify the form to satisfy either one of the given three approach:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <!-- Login form --> <form> <input type="hidden" name="login_hidden" value="login-whatever"> <input name="login_email" /> ... <input type="submit" value="Login" /> </form> <!-- Register form --> <form> <input type="hidden" name="register_hidden" value="register-whatever"> <input name="register_email" /> ... <input type="submit" value="Register" /> </form> |
Now under your controller/action, you can check either for:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | <?php // Use either one // Check for hidden input field if ($this->input->post('login_hidden')) { // Set rule for login form and run the form validation } // OR // Check for unique field if ($this->input->post('login_email')) { // Set rule for login form and run the form validation } // OR // Check for button submitted. We are using this if ($this->input->post('submit') == 'Login') { // Set rule for login form and run the form validation $this->form_validation->set_rules('YOUR RULES'); // Run the form if ($this->form_validation->run()) { // Process form } else { $data['login_errors'] = validation_errors(); } } elseif ($this->input->post('submit') == 'Register') { // Set rule for register form and run the form validation $this->form_validation->set_rules('YOUR RULES'); // Run the form if ($this->form_validation->run()) { // Process form } else { $data['register_errors'] = validation_errors(); } } |
And instead of using validation_errors() directly, you can assign errors to different variables, pass them to view and check if they exist.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <!-- Login form --> <form> if (isset($login_errors)) echo $login_errors; <input type="hidden" name="login_hidden" value="login-whatever"> <input name="login_email" /> ... <input type="submit" value="Login" /> </form> <!-- Register form --> <form> if (isset($register_errors)) echo $register_errors; <input type="hidden" name="register_hidden" value="register-whatever"> <input name="register_email" /> ... <input type="submit" value="Register" /> </form> |
Now in this way you can correctly process the forms and deal with multiple forms on one page / controller and form validation.
Hi! Can you show an example on how to have a “contact form” and “subscribe newsletters” on page?
Thanks