[eluser]doehoe[/eluser]
This potential bug has been tested in CodeIgniter 1.7.0 and PHP 5.2.5
If you use
set_radio and
set_checkbox (in the form helper) with the
$value parameter set to
0 (zero), it doesn't matter if the checkbox or radio field to which the set_radio or set_checkbox refers, it will never return the
checked="checked" if the the checkbox or radio fields were selected.
Two files illustrate:
The controller:
problemform.php
Code:
<?php
class ErrorForm extends Controller {
function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('textfield','','required');
$this->form_validation->set_rules('radio_1', '', '');
$this->form_validation->set_rules('check_1', '', '');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('problem_view');
}
else
{
//success
}
}
}
?>
The view:
problem_view.php
Code:
<html>
<head>
<title>My Form</title>
</head>
<body>
<?php echo form_open('errorform'); ?>
Text Field:
<input type="text" name="textfield" /><br /><br />
Radio Choice:
<input type="radio" name="radio_1" value="0" <?=set_radio('radio_1',0) ?>/>Choice 1
<input type="radio" name="radio_1" value="1" <?=set_radio('radio_1',1) ?>/>Choice 2<br /><br />
Checkbox:
<input type="checkbox" name="check_1" value="0" <?=set_checkbox('check_1',0) ?> /><br /><br />
<div><input type="submit" value="Submit" /></div>
</form>
</body>
</html>
To show the problem, run the controller and leave the Text Field blank (in order to generate a validation error). Now try select the "Choice 1" radio selection and click submit. When the form is redisplayed, Choice 1 should be checked, but it is not. But if you choose Choice 2, on submission, Choice 2 will be checked.
Similar for the checkbox. This only occurs because their "value" attributes are 0.
I have identified the problem for set_radio on
line 760 of
helpers/form_helper.php and on
line 812 of
libraries/Form_validation.php
Similarly for set_checkbox on
line 705 of
helpers/form_helper.php and on
line 856 of
libraries/Form_validation.php
The problem being the double equals ( == ) in
Code:
($field == '' OR $value == '')
Since $field will be 0, and
resolves to TRUE, the line should be
Code:
($field === '' OR $value === '')
i.e. a triple equals sign.
Please let me know if I should open a bug report.