[eluser]TWP Marketing[/eluser]
[quote author="kingmaoam" date="1344977345"]Hi,
in my current project I have a view where I generate checkboxes from a database table.
In the view I populate the checkboxes via
Code:
for($i = 0; $i < count($types); $i++)
{
$types[$i]['formAttr']['name'] = 'types[]';//= 't_'.$types[$i]['typeID'];
$types[$i]['formAttr']['id'] = 't_'.$types[$i]['typeID'];
$types[$i]['formAttr']['class'] = '';
$types[$i]['formAttr']['value'] = $types[$i]['typeID'];
if($this->input->post('t_'.$types[$i]['typeID']) == $types[$i]['typeID'])
{
$types[$i]['formAttr']['checked'] = 'checked';
}
}
<? foreach($types as $t) { ?>
<tr>
<td colspan='2'><?=form_checkbox($t['formAttr']); ?> <?=form_label($t['typeName'], $t['formAttr']['id']); ?></td>
</tr>
<? } ?>
´
In the controller function which the form is posted to I have a set of validation rules for the submitted data.
I want to have something like a rule which says: minimum 1 of the checkboxes must be activated.
One way could be to get all the values again from the database but is there a way without reading the data again?
KR
Habib[/quote]
Based on the unique attribute 'id', write multiple sets of validation rules for each possible 'id'.
use a switch statement in your controller to select the correct set of rules.
Code:
$id = $this->input->post('id'); // read the id
switch( $id )
{
case '1':
$this->form_validation->set_rules('username', 'Username', 'required');
break;
case '2':
$this->form_validation->set_rules('item2', 'Cost', 'required');
break;
default:
// this is probably an error state since no id matches a case
break;
}
}