Welcome Guest, Not a member yet? Register   Sign In
Howto require just one element of a form?
#1

[eluser]wdcmatt[/eluser]
I have an html form that is 20 check boxes. (A list of user groups...)

I have to ensure that AT LEAST ONE is checked, more is fine, but any of them is acceptable. (I must ensure that each user is a member of at least 1 group... they can be members of more but must have at least one group.)

I know ci has the "required" tag in form validation is there something handy I could use to require one out of a list of variables? I know how to do this with standard php, but was just curious if there was a nice feature of ci that could manage this for me.

Thanks in advance.
wdcmatt
#2

[eluser]OliverHR[/eluser]
You must use a custom validation, I mean your own callback function to see if at least one item is checked.

See this:

Callbacks: Your own Validation Functions
#3

[eluser]theshiftexchange[/eluser]
This does it - it forces uses to check AT LEAST one of the 3 checkboxes below. The key is you need them as an "array" - not as individual checkbox variables.

Controller:
Code:
$this->form_validation->set_rules('sector_list[]','Sector Selection', 'required');
if ($this->form_validation->run())
{
          // Worked - do something
}
else
{
          // Didnt work - reload the page
}


View:
Code:
<input type="checkbox" name="sector_list[]" value="road_north" <?php echo set_checkbox('sector_list[]', 'road_north', $road_north); ?> /></td>

<input type="checkbox" name="sector_list[]" value="road_south" <?php echo set_checkbox('sector_list[]', 'road_south', $road_south); ?> /></td>

<input type="checkbox" name="sector_list[]" value="road_west" <?php echo set_checkbox('sector_list[]', 'road_west', $road_west); ?> /></td>
#4

[eluser]wdcmatt[/eluser]
Good thinkin'

I like. I'll give that a shot. Thanks.




Theme © iAndrew 2016 - Forum software by © MyBB