Welcome Guest, Not a member yet? Register   Sign In
Form validation callback for two dependent form fields
#1

[eluser]Unknown[/eluser]
I have a form with one select box and two input fields. In the select box you can choose between option a and b. If the user selects option b, he has to enter some data into inputfield X, if option a is selected the field X is optional.

How can I add a form validation rule for this? I tried to use a own callback on filed X, but there I have only access the value of field X, right?

Maybe it is a simple problem, but I'm a freshman in CodeIgniter Wink

Thanks
#2

[eluser]TheFuzzy0ne[/eluser]
Welcome to the CodeIgniter forums!

I like to add rules on a per requirement basis.
Code:
$this->form_validation->set_rules('field1', 'Field 1', 'callback_somecallback');
if ($this->input->post('field1')) // The user must fill in the second field.
{
    $this->form_validation->set_rules('field2', 'Field 2', 'required|callback_anothercallback');
}

This way the rule is only added if something is entered into the first field.




Theme © iAndrew 2016 - Forum software by © MyBB