CodeIgniter Forums
Form Validation: using set_rules as conditional? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Form Validation: using set_rules as conditional? (/thread-42280.html)



Form Validation: using set_rules as conditional? - El Forum - 06-01-2011

[eluser]bpdp[/eluser]
hello everyone,
I have something like this:
Code:
$this->form_validation->set_rules('password', 'Password', 'trim|required|callback_correct_creds');
$this->form_validation->set_rules('email', 'Email', 'trim|required|callback_is_confirmed');

which currently kinda works but not exactly what I would like. right now I have a callback to check to see if the username and password combo is legit, and another call back to make sure that they have confirmed there email. both work as expected but if the email is entered wrong both errors are triggered. I was wondering if there was a way to do something like:
Code:
if($this->form_validation->set_rules('password', 'Password', 'trim|required|callback_correct_creds'))
{
    $this->form_validation->set_rules('email', 'Email', 'trim|required|callback_is_confirmed');
}

Thanks in advance


Form Validation: using set_rules as conditional? - El Forum - 06-01-2011

[eluser]toopay[/eluser]
You can't do that. Once you set the validation rule on one field, if that field fail against the validation rules then the error message will generated, regardless other fields is valid or not.


Form Validation: using set_rules as conditional? - El Forum - 06-02-2011

[eluser]bpdp[/eluser]
okay so let me give a little example and maybe someone can tell me what I'm doing wrong:

Code:
if($this->form_validation->set_rules('password', 'Password', 'trim|required|callback_test'))
{
    $this->form_validation->set_rules('email', 'Email', 'trim|required|callback_test2');    
}

public function test()
{
        $this->form_validation->set_message('test', 'Test 1 Error');    
     return FALSE;
}
    
public function test2()
{
    $this->form_validation->set_message('test2', 'Test 2 Error');    
        return FALSE;
}


the result is both error messages show even though test() is returning false.


Form Validation: using set_rules as conditional? - El Forum - 06-02-2011

[eluser]CroNiX[/eluser]
Probably something like this would work:
Code:
$this->form_validation->set_rules('password', 'Password', 'trim|required|callback_test');
$this->form_validation->set_rules('email', 'Email', 'trim|required|callback_test2');

public function test()
{
     $this->form_validation->set_message('test', 'Test 1 Error');    
     $this->form_validation->test1 = FALSE;  //set a variable within the form validation that you can check within test2
     return FALSE;
}

public function test2()
{
    $this->form_validation->set_message('test2', 'Test 2 Error');    
    if($this->form_validation->test1 === FALSE)
    {
        //do something
    }
    return FALSE;
}



Form Validation: using set_rules as conditional? - El Forum - 06-02-2011

[eluser]bpdp[/eluser]
Thanks for all the replies. That kinda works but won't work for me. depending on how I do it I either get a "Unable to access an error message corresponding to your field name." error. or the email field doesn't repopulate when the user is returned to the form. I can't believe that this is not a common problem, seems like a very basic thing.