Welcome Guest, Not a member yet? Register   Sign In
Yet another set_checkbox() validation re-population question
#1

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

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

[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.
#4

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

[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.
#6

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

[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.




Theme © iAndrew 2016 - Forum software by © MyBB