Welcome Guest, Not a member yet? Register   Sign In
set_radio and set_checkbox have problems with value of 0
#11

[eluser]Unknown[/eluser]
I was reluctant to modify the core system.

But I was able to fix the bug by setting validation rules for my checkboxes.

For checkboxes that didn't require validation, I just set the rule to something inconsequential like (trim).

Hopefully this will save others the time that I lost.
#12

[eluser]Iverson[/eluser]
[quote author="Jonas G" date="1256229124"]This is also an issue in 1.7.2[/quote]

+1. And it's pissing me off :coolmad:
#13

[eluser]nomikos3[/eluser]
Yes, still buggy in 1.7.2.

But you must never patch the core system.

The only solution is set values as chars (not integers):

wrong:
Code:
set_radio('radio_1', 0)

cool:
Code:
set_radio('radio_1', '0')

So in the the first sample at the top, should be:

Code:
Radio Choice:
<input type="radio" name="radio_1" value="0" <?=set_radio('radio_1', '0') ?>/>Choice 1
&lt;input type="radio" name="radio_1" value="1" &lt;?=set_radio('radio_1', '1') ?&gt;/&gt;Choice 2<br /><br />

Checkbox:
&lt;input type="checkbox" name="check_1" value="0" &lt;?=set_checkbox('check_1', '0') ?&gt; /&gt;&lt;br /><br />
---

I know that is very painful typing quotes, but these kind of issues always occur in PHP, so you may take this tip as a rule.

o/\o
#14

[eluser]dmstudios[/eluser]
WORKAROUND

Quote:I was able to fix the bug by setting validation rules for my checkboxes.

For checkboxes that didn’t require validation, I just set the rule to something inconsequential like (trim).

Thanks Dr. Crypto, that's a great and simple workaround rather than modifying or extending the core.
#15

[eluser]kiranugur[/eluser]
[quote author="Dr Crypto" date="1259157052"]
For checkboxes that didn't require validation, I just set the rule to something inconsequential like (trim).

Hopefully this will save others the time that I lost.[/quote]

There is no need to set the rule to "trim" or smt else. Just leave it blank.

Code:
$this->form_validation->set_rules('category', 'Category', '');

This works fine for me.
#16

[eluser]rip_pit[/eluser]
i still have the same problem and nothing here didn't worked while i'm using v2.+

I finally fixed it by downloading and using the latest update of the lib (v2.3)
and using it as an extension of the main system lib (class MY_Form_validation extend CI_Form_validation)

Code available at :
https://github.com/EllisLab/CodeIgniter/...dation.php

Seems now fine and accept '0' as value.
hope it can help Wink
#17

[eluser]sparky672[/eluser]
As of this posting, using the latest CI (version 2.1.4), and also have this problem.

Already using quotation marks as per a previous comment...

Code:
&lt;input type="radio" name="active" value="1"  &lt;?php echo set_radio('active', '1') ?&gt; /&gt;
&lt;input type="radio" name="active" value="0"  &lt;?php echo set_radio('active', '0') ?&gt; /&gt;

I'm a little surprised this bug has lingered for so many years.

-----

As a workaround, I have to create an empty rule...

Code:
$this->form_validation->set_rules('active', 'Status', '');

-----

I'm open to any suggestions for a workaround cleaner than an empty validation rule. Thank-you.
#18

[eluser]CroNiX[/eluser]
Any of the form_helper functions that retrieve a form value need at least one validation rule set up. set_value(), set_select(), set_radio(), set_checkbox(), etc., or they can't get the value.

I don't know why you'd use an empty rule though. I'd at least make a callback function to see if that field was an int 0 or 1, or whatever allowable choices you have. If it goes in the database, it should be validated. What if I edited the form in my browser and sent "3" and that wasn't an allowable or valid choice? Your code would store it.
#19

[eluser]sparky672[/eluser]
[quote author="CroNiX" date="1384386338"]Any of the form_helper functions that retrieve a form value need at least one validation rule set up. set_value(), set_select(), set_radio(), set_checkbox(), etc., or they can't get the value.[/quote]

Good to know. Thank-you. Up to this point, this thread was a bit misleading in that regard.

[quote author="CroNiX" date="1384386338"]I don't know why you'd use an empty rule though. I'd at least make a callback function to see if that field was an int 0 or 1, or whatever allowable choices you have. If it goes in the database, it should be validated. What if I edited the form in my browser and sent "3" and that wasn't an allowable or valid choice? Your code would store it.[/quote]

That's a very good point to note, especially for anyone else that may be reading this thread. However, in this case, only somebody logged in as "admin" has access to my form.




Theme © iAndrew 2016 - Forum software by © MyBB