Welcome Guest, Not a member yet? Register   Sign In
form_validation and multiple forms
#1

[eluser]theprodigy[/eluser]
I apologize if this has already been asked. My quick forum search didn't turn up anything.

When running form_validation on a page that has multiple forms (both login and registration are on same page), how can I tell which form to run validation?

normally I:
Code:
$this->load->helper('form');
$this->load->library('form_validation');
$this->form_validation->set_rules('field','field name','rules');
$this->form_validation->set_rules('field2', 'field2 name', 'more rules');
But the rules being set depend on which form is being used. Do I just set the rules for all form fields regardless of what form they are in?

I know that I can separate which set of rules to call when running $this->form_validation->run(), but with multiple forms on the page, do I call it twice (or as many times as there are forms on the page)? do I do
Code:
if($this->form_validation->run('login') == FALSE && $this->form_validation->run('register') == FALSE)
?

Thanks for any and all help
#2

[eluser]Phil Sturgeon[/eluser]
Form_validation library just checks the $_POST super-global variable so whatever in there is validated.

To find out which form was posted, you can either set the action attribute to go to two totally different controller methods (URL's) or you can do it based on the submit button.

Code:
if($this->input->post('loginSubmit'))
{
  // .. validate login
}

elseif( $this->input->post('registerSubmit')
{
  // .. validate registration
}

Those would just be set with:

Code:
<input type="submit" name="loginSubmit" id="loginSubmit" />

or of course use the form helper. It's all the same. :-)
#3

[eluser]theprodigy[/eluser]
I thought about sending them to two different controller functions, but wouldn't that make it difficult should they fail the validation?

-----Example:-----
http://www.example.com shows two forms.
One goes to /login and one goes to /register.
They fill out the registration form, and click submit.
Oops! password is not long enough.
What do I do now?
Do I redirct back to http://www.example.com passing form field data back to re-populate the form?
Do I leave it at /register but show the same view over again, so that both /register and / are essentially the same page (and same with /login)?
Something else entirely I haven't mentioned?
-----End Example-----

If I don't send to different functions, then how would I handle the multiple form submissions?
I thought about doing the
Code:
if($this->input->post('login'))
and same for register, but first time through, neither one are submitted. Wouldn't that cause
Code:
<?= validation_errors(); ?>
to throw an error? (not sure, haven't tried, but seems likely since there was no validation performed).

Thanks
#4

[eluser]Phil Sturgeon[/eluser]
Code:
<?php if(validation_errors()) echo validation_errors(); ?>
;-)




Theme © iAndrew 2016 - Forum software by © MyBB