Welcome Guest, Not a member yet? Register   Sign In
Form validation waterfall
#1

[eluser]summery[/eluser]
I'd like one controller function to handle two forms in a waterfall. But after checking one set of form validation rules, CI doesn't seem to check the second set of validation rules, and just returns "both forms passed validation" -- which they have not.

Here's the structure of the code:

Code:
if ($this->form_validation->run('rule_set_1') == FALSE)  {
  $this->load->view('form_part_1');
}
        
else if ($this->form_validation->run('rule_set_2') == FALSE)  {
  $this->load->view('form_part_2');
}

else {
  echo "Both forms passed validation";
}

Ideas? I know I could split this up among multiple functions, but this approach seems cleaner. I'd like to get it to work.
#2

[eluser]summery[/eluser]
I started mucking around in the CI Core and I've found the problem.

In libraries/Form_validation.php, the first thing the run() function does is check if validation rules are set in the _field_data array. If they are set, it simply checks the form against those rules, paying ZERO attention to its argument, which is (in this case) a new and different set of rules to apply. Annoying.

There are two approaches to fixing it, and both involve overriding form validation's run() function in the CI core.
1) Mess with the beginning of the function, so the validator pays attention to its argument first, its internal state and default configuration rules second.
2) Empty the _field_data array when validation passes.

I think I'm going with #2.

Fingers crossed!! :-)
#3

[eluser]summery[/eluser]
Method #2 did not work very well - clearing the _field_data array meant that none of CI's form_validation->set_value functions worked anymore.

Method #1 worked great; I just copied CI's run() function and changed it a little bit so that it would read function arguments first and add those to the rules.

Now I have beautiful additive multi-part forms (multi-screen forms, really) handled by one controller!

Smile




Theme © iAndrew 2016 - Forum software by © MyBB