Welcome Guest, Not a member yet? Register   Sign In
Run multiple form validation from config file
#1

[eluser]ChristiaanT[/eluser]
Hello,

I'm using CI for almost 2 years now and this is the first time I ever post something in here. It proofs how easy CI is! But...

Currently I'm working on a site that has a lot forms to display. All the forms starting with standard personal data. So I thought to have a standard view for this personal data and for every other form I'm loading another form view to. Works perfect.

Now I'm handling the data after posting it, but now I want to validate the personal fields AND the other fields at once. I made a form_validation.php config file with all rules and fields set up in different sections (so 'default' for the default fields, and 'blabla' for the blabla fields). But when I try to run multiple validations it won't work. I'm doing like this:

form_validation.php config file

Code:
$config = array(
    'default' => array(
        array(
            'field' => 'person_name',
            'label' => 'Name',
            'rules' => 'required'
            ),
        array(
            'field' => 'person_email',
            'label' => 'Email',
            'rules' => 'required'
            ),
        ),
    'blabla' => array(
        array(
            'field' => 'blabla_date',
            'label' => 'Blabla date',
            'rules' => 'required'
            ),

Controller
Code:
function send($formname)
    {        
        $data['formname'] = $formname;

        if ($this->form_validation->run('default') == FALSE && $this->form_validation->run($formname) == FALSE){
                        
            $this->load->view('default/header', $data);
            $this->load->view('form/form_persondata', $data);
            $this->load->view('form/form_'.$formname, $data);    
            $this->load->view('form/form_footer', $data);
            $this->load->view('default/footer');

        } else {

It does handle the first run (default), but after that it won't run the second. Or at least it doesn't give me the errors when I'm not filling in the form complete.

Do you guys have a clue how to handle this? A solution would be to put all the rules and fields in one section, but I don't like to have double data in my app. Another solution would be to array_merge the two arrays from the form_validation.php config file and then 'load' the config file. But I couldn't find a way to do this. The previous version did it that way, didn't it?

I hope you can help me out. Thanks...
#2

[eluser]helmutbjorg[/eluser]
I think I understand your question... When doing a comparison using && or || PHP will only work as hard as it has to.

For example

Code:
// For this example we'll set $var equal to 1
$var = 1;

// In this condition both checks will occur
if($var == 1 && $var == 2) { }

// In this condition only the first one will be checked because the condition is already true and
// php will save time becuase it no longer matters if $var == 2 because it won't change the result
if($var == 1 || $var == 2) { }

// In this condition both vars will be checked because the condition may still be true
if($var == 2 || $var == 1) { }

Hope that helps!
#3

[eluser]helmutbjorg[/eluser]
So to fix your problem you would do

Code:
function send($formname)
    {        
        $data['formname'] = $formname;

        $result1 = $this->form_validation->run('default');
        $result2 = $this->form_validation->run($formname);

        if ($result1 == FALSE && $result2 == FALSE){
                        
            $this->load->view('default/header', $data);
            $this->load->view('form/form_persondata', $data);
            $this->load->view('form/form_'.$formname, $data);    
            $this->load->view('form/form_footer', $data);
            $this->load->view('default/footer');

        } else {

So that both validations run even if the if condition doesn't get fully checked.
#4

[eluser]ChristiaanT[/eluser]
Thanks for your reply helmutbjorg, but I'm sorry... I made a mistake in my first post. When I made up the post I did enter && instead of ||.

So I already tried this (with the || instead of &&):

Code:
function send($formname)
    {        
        $data['formname'] = $formname;

        if ($this->form_validation->run('default') == FALSE || $this->form_validation->run($formname) == FALSE){
                        
            $this->load->view('default/header', $data);
            $this->load->view('form/form_persondata', $data);
            $this->load->view('form/form_'.$formname, $data);    
            $this->load->view('form/form_footer', $data);
            $this->load->view('default/footer');

        } else {

But this won't work too. I did another check like this:

Code:
function send($formname)
    {        
        $data['formname'] = $formname;

        if ($this->form_validation->run('default') == FALSE){
                        
            $this->load->view('default/header', $data);
            $this->load->view('form/form_persondata', $data);
            $this->load->view('form/form_'.$formname, $data);    
            $this->load->view('form/form_footer', $data);
            $this->load->view('default/footer');

        } else {
            if($this->form_validation->run($formname) == FALSE) {
                 $this->load->view('default/header', $data);
                 $this->load->view('form/form_persondata', $data);
                 $this->load->view('form/form_'.$formname, $data);    
                 $this->load->view('form/form_footer', $data);
                 $this->load->view('default/footer');
            } else {
               // Submit the data
            }
        }

But this won't work too. It runs the first and (maybe) checks the second, but I don't get an error from the form_validation class.

Nobody ever had this problem too? I thought i'd give it a try on this forum...but no luck at this point.

Update: I'll give it a try when I'm at home... I didn't read your post that good enought. I hope there's still hope Smile I'll post the results later today...
#5

[eluser]ChristiaanT[/eluser]
Here I am back again with the results. Still it won't work. I have tried the way with && like mentioned earlier, but no results.

I also tried to store the validation_error() into a variable after the 'default' validation run, and after the 'formname' validation run add those errors (from validation_error()) to this variable. I get two times the validation_error() string from the first (default) run.

Is there anyone who has a solution for my problem?
#6

[eluser]davidbehler[/eluser]
Add something like this to your form_validtion config file
Code:
foreach($config['default'] as $rule)
{
  $config['blabla'][] = $rule;
}
and now only run the 'blabla' validation.

€dit:
Have you looked at your log file? Maybe the validation library has encountered some problem.
#7

[eluser]ChristiaanT[/eluser]
Waldmeister! Great work! I never thought about that! I did mention to array_merge the two, but couldn't find a way where to do that. Thanks for your solution!

But still I'm wondering why you can't run two validations by default. Shouldn't this be a native supported function in CI? Or maybe a bug?
#8

[eluser]davidbehler[/eluser]
I thought about array_merge aswell but values with the same keys would be overridden...that's not what you need.

Have you looked at the log file? Anything in there?
#9

[eluser]ChristiaanT[/eluser]
Hmm, no the log tells me nothing special. But I'm not going to dive into this problem anymore, I'm glad you helped me out! It took me too much time for for now Smile
#10

[eluser]Ki[/eluser]
Got the same problem!
CI will only check the first validation->run('name1') and not the second... I do get the errors from either validation rule set in my config file, but only errors for the first validation.
Examples:
1) This will give you errors for rules set in name1 validation array. If no errors encountered, name 2 will not be checked... if errors exist, name 2 is not checked, even if I enter erroneous input
$this->validation->run('name1');
$this->validation->run('name2');

2) This will give you errors for rules set in name2 validation array. If no errors encountered, name1 will not be checked... if errors exist, name1 is not checked, even if I enter erroneous input
$this->validation->run('name2');
$this->validation->run('name1');

Can someone comment on this? Can you really run only 1 form validation per controller?




Theme © iAndrew 2016 - Forum software by © MyBB