CodeIgniter Forums

Full Version: Form Validation: using set_rules as conditional?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[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

El Forum

[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.

El Forum

[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.

El Forum

[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;
}

El Forum

[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.