CodeIgniter icon indicating copy to clipboard operation
CodeIgniter copied to clipboard

problem with set_radio function

Open dcr2018 opened this issue 8 years ago • 5 comments

Hi, this is a problem that appears in CI 3. When I fill and submit a form, and the validation fails, the form is repopulated properly with set_value, set_select and set_radio functions. But when I'm filling the form programatically set_radio fails. Let me explain myself by telling you what happens in my application.

Suppose I have an "update person's data" link in my application. When the user opens the link he receives a form with a list of people to choose from. His option is sent through POST, and then he gets a second form populated with person's data. In this form all the fields are populated properly, except the radio fields. This happens only if I'm coming from a POST request. If I change the application so the first form uses GET method, it works fine.

I'm attaching two files showing the first and the second form. screen 1 screen 2

dcr2018 avatar Mar 13 '18 14:03 dcr2018

Sorry, but this is very unclear. And we're talking about code, so screenshots are useless ... You need to clarify with a short code example.

narfbg avatar Mar 15 '18 14:03 narfbg

Ok, I made a short code example. The example is about adding and editing data about a person. There is a controller, Person, and three views. The first view is insert_form.php, where the user is able to add data into the database. The second view, list_records.php, shows a list of people and the user choose a person to edit it's data. After that, he gets the third view, update_form, where the user gets a form populated with the person's data he choose. Here the user is able to make changes to a person.

For the update form, I'm using set_value and set_radio with the DEFAULT parameter to show the form populated with data. The function set_radio is not working.

Here is the code: set_radio-5436.zip

dcr2018 avatar Mar 17 '18 23:03 dcr2018

A short example doesn't need 3 views, lenghty explanations or be zipped ... This is a short example:

$_SERVER['REQUEST_METHOD'] = 'POST';
$_POST['field'] = 'value';

// Return expected: ' checked="checked"'
// Return actual: ''
set_radio('field', 'value');

narfbg avatar Mar 18 '18 22:03 narfbg

Your file update_form.php is incorrect.

Add form_close() and use form_radio() helper and works!

<?php
echo validation_errors();
echo form_open('person/event_update_person');
?>
<input type="hidden" name="personId" value="<?= $person[0]->id?>">
<div><label style="display: inline-block;width: 120px;">Name</label>
<input type="text" name="name" value="<?= set_value('name', $person[0]->name);?>"></div>
<div><br><label style="display: inline-block;width: 120px;">Size</label>
	<?php echo form_radio('size','s',$person[0]->size == 's'); ?> S&nbsp;&nbsp;
	<?php echo form_radio('size','m',$person[0]->size == 'm'); ?> M&nbsp;&nbsp;
	<?php echo form_radio('size','l',$person[0]->size == 'l'); ?> L&nbsp;&nbsp;
</div>
<br><input type="submit" value="Save">
<?php echo form_close(); ?>

davidgv88 avatar Nov 27 '18 13:11 davidgv88

@narfbg fixed. This is not a issue.

davidgv88 avatar Nov 27 '18 13:11 davidgv88