CodeIgniter Forums
Yet another set_checkbox() validation re-population question - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Yet another set_checkbox() validation re-population question (/showthread.php?tid=32155)



Yet another set_checkbox() validation re-population question - El Forum - 07-14-2010

[eluser]helloworldly[/eluser]
I see a bunch of posts about checkbox validation re-population but they seem to be either outdated (pre latest edition of CI), do not define the solution clearly, or do not apply exactly to this case.

I'm using CI's form validation and it works elsewhere but I can't get set_checkbox() working when using multiple checkboxes as an array.

Within the form within the view:
Code:
<input type="checkbox" value="ABC" name="group[]" <?php echo $this->validation->set_checkbox('group[]', 'ABC'); ?> />
<label>ABC</label><br/>

&lt;input type="checkbox" value="XYZ" name="group[]" &lt;?php echo $this-&gt;validation->set_checkbox('group[]', 'XYZ'); ?&gt; />
<label>XYZ</label><br/>

// This doesn't work either:
&lt;input type="checkbox" value="XYZ" name="group[]" &lt;?php echo $this-&gt;validation->set_checkbox('group', 'XYZ'); ?&gt; />

Controller:
Code:
// Assign rules
$rules['group[]'] = "required";
$rules['anothergroup[]'] = "required";

// This doesn't work either:
$rules['group'] = "required";

$this->validation->set_rules($rules);

Thanks for any help.


Yet another set_checkbox() validation re-population question - El Forum - 07-14-2010

[eluser]ram4nd[/eluser]
can't put [] in variable name, learn basics of programming or php


Yet another set_checkbox() validation re-population question - El Forum - 07-14-2010

[eluser]danmontgomery[/eluser]
[quote author="ram4nd" date="1279157088"]can't put [] in variable name, learn basics of programming or php[/quote]

Congratulations, not only is this unhelpful and rude, it's patently untrue.



You should be using the set_checkbox() function, not validation->set_checkbox. On top of that, the validation library is deprecated (removed in 2.0), you should be using form_validation.

There's an example of how to do exactly what you want in the user guide.


Yet another set_checkbox() validation re-population question - El Forum - 07-15-2010

[eluser]ram4nd[/eluser]
[] behind variable name, is array. Or you can call chars from string. Not rude just a suggestion.


Yet another set_checkbox() validation re-population question - El Forum - 07-15-2010

[eluser]Yorick Peterse[/eluser]
[quote author="ram4nd" date="1279203104"][] behind variable name, is array. Or you can call chars from string. Not rude just a suggestion.[/quote]

And since the user is using an ARRAY of checkboxes, hence the [] behind the value of the name tag, this works perfectly fine.

Code:
&lt;input type="checkbox" name="foo[]" value="foo" /&gt;
&lt;input type="checkbox" name="foo[]" value="foo 1" /&gt;

This would result in the following POST array:

Code:
foo = Array(
[0] => foo,
[1] => foo 1
)

Also, the userguide actually states that you should use the brackets. So instead of acting you know anything, learn something.


Yet another set_checkbox() validation re-population question - El Forum - 07-15-2010

[eluser]ram4nd[/eluser]
interesting... True I have never used checkboxes


Yet another set_checkbox() validation re-population question - El Forum - 07-19-2010

[eluser]helloworldly[/eluser]
Noctrum, thanks so much. Yes, I hadn't realized that info I had gleaned from forum to build out previous form was based on outdated library. I'm using the new "form_validation" library - vs the old "validation" library. Everything is good. Thanks.