Welcome Guest, Not a member yet? Register   Sign In
beginner validation question
#1

[eluser]new_igniter[/eluser]
Hello,
I am new to CI and would love to know some existing approaches to making a form_input required only when a user checks a form_checkbox

Basically, I want to give my users the option to create a new group by checking a box. Thus, it IS OPTIONAL. But I want to require they name it, only after they have checked the box.

Can anyone offer an example to know how CI likes this to happen?

Many thanks!
Pete
#2

[eluser]Phil Sturgeon[/eluser]
Can be done with some concatenation to the rule. Check the following example:

Code:
$rules = array(
'checkbox' => 'numeric'
'textinputfield' => 'max_length[100]'
);

if($this->input->post('checkbox') == 1 ):
$rules['textinputfield'] .= '|required';
endif;

This basically is a dynamic form rule, added after your normal rule array declaration. If the checkbox is ticked then it adds 'required' to the rules.
#3

[eluser]tonanbarbarian[/eluser]
or you could write your own validation callback to do the check for you
#4

[eluser]new_igniter[/eluser]
[quote author="thepyromaniac" date="1198819660"]Can be done with some concatenation to the rule. Check the following example:

Code:
$rules = array(
'checkbox' => 'numeric'
'textinputfield' => 'max_length[100]'
);

if($this->input->post('checkbox') == 1 ):
$rules['textinputfield'] .= '|required';
endif;

This basically is a dynamic form rule, added after your normal rule array declaration. If the checkbox is ticked then it adds 'required' to the rules.[/quote]

so I have to declare the rule first?
#5

[eluser]new_igniter[/eluser]
thanks thepyromaniac, worked great.




Theme © iAndrew 2016 - Forum software by © MyBB