Repopulate radio buttons on failed validation Repopulate radio buttons on failed validation codeigniter codeigniter

Repopulate radio buttons on failed validation


There seems to be a bug. Unless you include a validation rule for the radio buttons, they do not get repopulated upon postback.

You can include a rule like this.

 $this->form_validation->set_rules('active', 'active', 'required');

if you had..

<div id="active-container">   <input type="radio" name="active" id="lesson-active" value="1" <?php echo set_radio('active', '1', TRUE); ?> />   <label for="lesson-active">Active</label>   <input type="radio" name="active" id="lesson-deactivated" value="2" <?php echo set_radio('active', '2'); ?> />   <label for="lesson-deactivated">Deactivated</label></div>


From the CodeIgniter UserGuide.

<input type="radio" name="myradio" value="1" <?php echo set_radio('myradio', '1', TRUE); ?> /><input type="radio" name="myradio" value="2" <?php echo set_radio('myradio', '2'); ?> />

The first parameter is the name of the radio set, the second is the current radio's value, and the third is an option default if there is no data to populate the field with.


After looking at several code examples, I noticed that the set_radio function HAD TO BE in the value parameter of the form_radio function. Since you can't have an echo within an echo, I created a "hack" by putting an empty string in the value parameter and then appending the set_radio function to it. Here is my example:

echo form_radio('gender', 'M', ''.set_radio('gender', 'M'), $js)."Male ";echo form_radio('gender', 'F', ''.set_radio('gender', 'F'), $js)."Female ";