Welcome Guest, Not a member yet? Register   Sign In
How to validate dynamic generated checkboxes
#1

[eluser]kingmaoam[/eluser]
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';
}
}

&lt;? foreach($types as $t) { ?&gt;
<tr>
<td colspan='2'>&lt;?=form_checkbox($t['formAttr']); ?&gt; &lt;?=form_label($t['typeName'], $t['formAttr']['id']); ?&gt;</td>
</tr>
&lt;? } ?&gt;
´

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
#2

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

&lt;? foreach($types as $t) { ?&gt;
<tr>
<td colspan='2'>&lt;?=form_checkbox($t['formAttr']); ?&gt; &lt;?=form_label($t['typeName'], $t['formAttr']['id']); ?&gt;</td>
</tr>
&lt;? } ?&gt;
´

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;
}
}
#3

[eluser]kingmaoam[/eluser]
Hi,

there was an error in my idea of solving the problem...
The requirement is to have a rule which says "it is required to click minimum one of the checkboxes"

so i have let's say 4 checkboxes of name t_1, t_2, t_3 and t_4
and if none of them is checked an error should be reported.

I think I have to find out manually in my verify-function whether min 1 checkbox is activated. But is there a way to report this error via the form_validation class? In the errors you always have to give a rule name... but in my case i would not have a rule defined...
#4

[eluser]TWP Marketing[/eluser]
Since the checkbox field would return an array of four choices, I don't know how the built-in validation rules may be used to check for one or more being checked. You can write a custom validation rule, but that may be more trouble than simply checking them after validation has been run.




Theme © iAndrew 2016 - Forum software by © MyBB