set_radio() function not repopulating my radio buttons. Can't figure out why - El Forum - 01-17-2012
[eluser]iamzvonko[/eluser]
Hello,
I have a form with several radio button groups. My problem is that I can't get it to re-populate the selected radio button in each group. Here's a snippet for one of the groups. The $days variable is an array of arrays. Each array entry contains some information about a specific day.
Code: <?php
echo "<b>Step 2: Select a date</b><br /> ";
foreach ($days as $day) {
$val = $day['dateID'];
?>
<input type="radio" value="<?php echo $val;?>" name="volDate" id="volDate"
<?php set_radio('volDate', $val);?>/><?php echo $day['day']; ?>
<?php } ?>
For text input fields I use the set_value('name') function and it works fine. Well, it works fine as long as I have a validation rule for each text field. Found out that I had to have an empty validation rule if in fact I didn't need to validate the field just so it would get repopulated.
Thinking along this line, I thought I needed to add an empty validation rule for the radio button so I added this:
Code: $this->form_validation->set_rules('volDate', 'Date', '');
Still no luck. When I submit the form and have to re-display it because of errors on other fields, my selected radio button is now un-selected.
Any idea how I can get around this?
Thanks!
set_radio() function not repopulating my radio buttons. Can't figure out why - El Forum - 01-17-2012
[eluser]iamzvonko[/eluser]
None that I can see. Here's the log file. The 5:23 entries are from initial load. The 5:25 entries are from the submit after I selected a radio button. Do you see anything that stands out as unusual in this?
Code: DEBUG - 2012-01-18 05:23:32 --> Config Class Initialized
DEBUG - 2012-01-18 05:23:32 --> Hooks Class Initialized
DEBUG - 2012-01-18 05:23:32 --> Utf8 Class Initialized
DEBUG - 2012-01-18 05:23:32 --> UTF-8 Support Enabled
DEBUG - 2012-01-18 05:23:32 --> URI Class Initialized
DEBUG - 2012-01-18 05:23:32 --> Router Class Initialized
DEBUG - 2012-01-18 05:23:32 --> Output Class Initialized
DEBUG - 2012-01-18 05:23:32 --> Security Class Initialized
DEBUG - 2012-01-18 05:23:32 --> Input Class Initialized
DEBUG - 2012-01-18 05:23:32 --> Global POST and COOKIE data sanitized
DEBUG - 2012-01-18 05:23:32 --> Language Class Initialized
DEBUG - 2012-01-18 05:23:32 --> Loader Class Initialized
DEBUG - 2012-01-18 05:23:32 --> Helper loaded: url_helper
DEBUG - 2012-01-18 05:23:32 --> Helper loaded: form_helper
DEBUG - 2012-01-18 05:23:32 --> Database Driver Class Initialized
DEBUG - 2012-01-18 05:23:33 --> Session Class Initialized
DEBUG - 2012-01-18 05:23:33 --> Helper loaded: string_helper
DEBUG - 2012-01-18 05:23:33 --> Session routines successfully run
DEBUG - 2012-01-18 05:23:33 --> XML-RPC Class Initialized
DEBUG - 2012-01-18 05:23:33 --> DX Auth Initialized
DEBUG - 2012-01-18 05:23:33 --> Session class already loaded. Second attempt ignored.
DEBUG - 2012-01-18 05:23:33 --> Config file loaded: application/config/dx_auth.php
DEBUG - 2012-01-18 05:23:33 --> Language file loaded: language/english/dx_auth_lang.php
DEBUG - 2012-01-18 05:23:33 --> Form Validation Class Initialized
DEBUG - 2012-01-18 05:23:33 --> Controller Class Initialized
DEBUG - 2012-01-18 05:23:33 --> Model Class Initialized
DEBUG - 2012-01-18 05:23:33 --> Model Class Initialized
DEBUG - 2012-01-18 05:23:33 --> Session class already loaded. Second attempt ignored.
DEBUG - 2012-01-18 05:23:53 --> File loaded: application/views/bookSlot.php
DEBUG - 2012-01-18 05:23:53 --> File loaded: application/views/templateVolunteer.php
DEBUG - 2012-01-18 05:23:53 --> Final output sent to browser
DEBUG - 2012-01-18 05:23:53 --> Total execution time: 20.9332
DEBUG - 2012-01-18 05:25:09 --> Config Class Initialized
DEBUG - 2012-01-18 05:25:09 --> Hooks Class Initialized
DEBUG - 2012-01-18 05:25:09 --> Utf8 Class Initialized
DEBUG - 2012-01-18 05:25:09 --> UTF-8 Support Enabled
DEBUG - 2012-01-18 05:25:09 --> URI Class Initialized
DEBUG - 2012-01-18 05:25:09 --> Router Class Initialized
DEBUG - 2012-01-18 05:25:09 --> Output Class Initialized
DEBUG - 2012-01-18 05:25:09 --> Security Class Initialized
DEBUG - 2012-01-18 05:25:09 --> Input Class Initialized
DEBUG - 2012-01-18 05:25:09 --> Global POST and COOKIE data sanitized
DEBUG - 2012-01-18 05:25:09 --> Language Class Initialized
DEBUG - 2012-01-18 05:25:09 --> Loader Class Initialized
DEBUG - 2012-01-18 05:25:09 --> Helper loaded: url_helper
DEBUG - 2012-01-18 05:25:09 --> Helper loaded: form_helper
DEBUG - 2012-01-18 05:25:09 --> Database Driver Class Initialized
DEBUG - 2012-01-18 05:25:10 --> Session Class Initialized
DEBUG - 2012-01-18 05:25:10 --> Helper loaded: string_helper
DEBUG - 2012-01-18 05:25:10 --> Session routines successfully run
DEBUG - 2012-01-18 05:25:10 --> XML-RPC Class Initialized
DEBUG - 2012-01-18 05:25:10 --> DX Auth Initialized
DEBUG - 2012-01-18 05:25:10 --> Session class already loaded. Second attempt ignored.
DEBUG - 2012-01-18 05:25:10 --> Config file loaded: application/config/dx_auth.php
DEBUG - 2012-01-18 05:25:10 --> Language file loaded: language/english/dx_auth_lang.php
DEBUG - 2012-01-18 05:25:10 --> Form Validation Class Initialized
DEBUG - 2012-01-18 05:25:10 --> Controller Class Initialized
DEBUG - 2012-01-18 05:25:10 --> Model Class Initialized
DEBUG - 2012-01-18 05:25:10 --> Model Class Initialized
DEBUG - 2012-01-18 05:25:10 --> Session class already loaded. Second attempt ignored.
DEBUG - 2012-01-18 05:25:15 --> File loaded: application/views/bookSlot.php
DEBUG - 2012-01-18 05:25:15 --> File loaded: application/views/templateVolunteer.php
DEBUG - 2012-01-18 05:25:15 --> Final output sent to browser
DEBUG - 2012-01-18 05:25:15 --> Total execution time: 6.1827
set_radio() function not repopulating my radio buttons. Can't figure out why - El Forum - 01-17-2012
[eluser]Matt S.[/eluser]
Funny how something so simple could cause so much confusion...you forgot to add an echo before set_radio()!
Code: <?php echo set_radio('volDate', $val);?>/>
set_radio() function not repopulating my radio buttons. Can't figure out why - El Forum - 01-17-2012
[eluser]iamzvonko[/eluser]
Oh man, just shoot me now!
Thanks
set_radio() function not repopulating my radio buttons. Can't figure out why - El Forum - 01-18-2012
[eluser]iamzvonko[/eluser]
Well, not so fast. I was really excited when I saw that last reply last night because it seemed like just a stupid programmer error. I can deal with that. I had shut down my PC by then and I haven't been able to get back to it until now (gotta work for a living during the day).
So I put in the echo but STILL no luck. Then I went back to basics. I built the form controller and view as described in CI documentation here.
I just added some radio buttons explicitly (without loop as in my code) and it repopulated the proper radio button on submit. Then I added another radio button group but generated it with a loop this time. Worked like a charm also. As long as I had an empty validation rule in controller.
That made me think "maybe I just typed something wrong in my validation rule". Checked and checked but I just don't see it.
Sorry for the long post but I'm pulling out what little hair I have left. Any help would be greatly appreciated.
Here's the sample form controller and view that works just fine
FORM CONTROLLER
Code: <?php
class Form extends CI_Controller {
function index() {
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email', 'required');
$this->form_validation->set_rules('volDate', 'volDate', '');
$this->form_validation->set_rules('volDate2', 'volDate2', '');
if ($this->form_validation->run() == FALSE) {
$this->load->view('myform');
} else {
$this->load->view('formsuccess');
}
}
}
?>
FORM VIEW
Code: <html>
<head>
<title>My Form</title>
</head>
<body>
<?php echo validation_errors(); ?>
<?php echo form_open('form'); ?>
<h5>Email Address</h5>
<input type="text" name="email" value="<?php echo set_value('email'); ?>" size="50" />
<h5>Explicit</h5>
<input type="radio" value="Mon" name="volDate" id="volDate" <?php echo set_radio('volDate', "Mon");?>/>Monday
<input type="radio" value="Tue" name="volDate" id="volDate" <?php echo set_radio('volDate', "Tue");?>/>Tuesday
<input type="radio" value="Wed" name="volDate" id="volDate" <?php echo set_radio('volDate', "Wed");?>/>Wednesday
<input type="radio" value="Thu" name="volDate" id="volDate" <?php echo set_radio('volDate', "Thu");?>/>Thurday
<input type="radio" value="Fri" name="volDate" id="volDate" <?php echo set_radio('volDate', "Fri");?>/>Friday
<h5>With Loop</h5>
<?php
$days2 = array("dateID" => 1, "day" => "Mon");
array_push($days2, array("dateID" => 2, "day" => "Tue"));
array_push($days2, array("dateID" => 3, "day" => "Wed"));
array_push($days2, array("dateID" => 4, "day" => "Thu"));
array_push($days2, array("dateID" => 5, "day" => "Fri"));
foreach ($days2 as $day2) {
$val2 = $day2['dateID'];
?>
<input type="radio" value="<?php echo $val2;?>" name="volDate2" id="volDate2"<?php echo set_radio('volDate2', $val2);?>/><?php echo $day2['day']; ?>
<?php } ?>
<div><input type="submit" value="Submit" /></div>
</form>
</body>
</html>
Now here are snippets of my code that does NOT work:
CONTROLLER
Code: ...
$this->form_validation->set_rules('volDate', 'Date', '');
$this->form_validation->set_rules('volSite', 'Site', '');
...
VIEW
Code: ...
<?php
echo "<b>Step 1: Select a date</b><br /> ";
foreach ($days as $day) {
$val = $day['dateID'];
?>
<input type="radio" value="<?php echo $val;?>" name="volDate" id="volDate"<?php echo set_radio('volDate', $val);?>/><?php echo $day['day']; ?>
<?php } ?>
<br>
<?php
echo "<br /><b>Step 2: Select a location</b><br /> ";
foreach ($sites as $site) {
$val = $site['siteID'];
echo '<input type="radio" value="'.$val.'" name="volSite" id="volSite">'.$site['name'].set_radio('volSite', $val).' ';
}
?>
...
set_radio() function not repopulating my radio buttons. Can't figure out why - El Forum - 01-19-2012
[eluser]Philip Kavanagh[/eluser]
If you want to validate a radio input, you should check for the "on" status rather than the "checked".
Simply create a new callback or extend CI_Form_validation lib.
you would build a proper callback here, but im just making it clear here...
Code: function is_on($input)
{
return ($input === 'on') ? TRUE : FALSE;
}
set_radio() function not repopulating my radio buttons. Can't figure out why - El Forum - 01-19-2012
[eluser]iamzvonko[/eluser]
I'm not really concerned with validating it. I just want it to repopulate the correct radio button in the group when I submit the form and come back to it. Or am I misunderstanding what you're suggesting?
|