Welcome Guest, Not a member yet? Register   Sign In
set_radio() function not repopulating my radio buttons. Can't figure out why
#1

[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 />&nbsp;&nbsp;";
foreach ($days as $day) {
  $val = $day['dateID'];
?&gt;
&lt;input type="radio" value="&lt;?php echo $val;?&gt;" name="volDate" id="volDate"
&lt;?php set_radio('volDate', $val);?&gt;/&gt;&lt;?php echo $day['day']; ?&gt;&nbsp;&nbsp;
&lt;?php } ?&gt;

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!
#2

[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 --&gt; Config Class Initialized
DEBUG - 2012-01-18 05:23:32 --&gt; Hooks Class Initialized
DEBUG - 2012-01-18 05:23:32 --&gt; Utf8 Class Initialized
DEBUG - 2012-01-18 05:23:32 --&gt; UTF-8 Support Enabled
DEBUG - 2012-01-18 05:23:32 --&gt; URI Class Initialized
DEBUG - 2012-01-18 05:23:32 --&gt; Router Class Initialized
DEBUG - 2012-01-18 05:23:32 --&gt; Output Class Initialized
DEBUG - 2012-01-18 05:23:32 --&gt; Security Class Initialized
DEBUG - 2012-01-18 05:23:32 --&gt; Input Class Initialized
DEBUG - 2012-01-18 05:23:32 --&gt; Global POST and COOKIE data sanitized
DEBUG - 2012-01-18 05:23:32 --&gt; Language Class Initialized
DEBUG - 2012-01-18 05:23:32 --&gt; Loader Class Initialized
DEBUG - 2012-01-18 05:23:32 --&gt; Helper loaded: url_helper
DEBUG - 2012-01-18 05:23:32 --&gt; Helper loaded: form_helper
DEBUG - 2012-01-18 05:23:32 --&gt; Database Driver Class Initialized
DEBUG - 2012-01-18 05:23:33 --&gt; Session Class Initialized
DEBUG - 2012-01-18 05:23:33 --&gt; Helper loaded: string_helper
DEBUG - 2012-01-18 05:23:33 --&gt; Session routines successfully run
DEBUG - 2012-01-18 05:23:33 --&gt; XML-RPC Class Initialized
DEBUG - 2012-01-18 05:23:33 --&gt; DX Auth Initialized
DEBUG - 2012-01-18 05:23:33 --&gt; Session class already loaded. Second attempt ignored.
DEBUG - 2012-01-18 05:23:33 --&gt; Config file loaded: application/config/dx_auth.php
DEBUG - 2012-01-18 05:23:33 --&gt; Language file loaded: language/english/dx_auth_lang.php
DEBUG - 2012-01-18 05:23:33 --&gt; Form Validation Class Initialized
DEBUG - 2012-01-18 05:23:33 --&gt; Controller Class Initialized
DEBUG - 2012-01-18 05:23:33 --&gt; Model Class Initialized
DEBUG - 2012-01-18 05:23:33 --&gt; Model Class Initialized
DEBUG - 2012-01-18 05:23:33 --&gt; Session class already loaded. Second attempt ignored.
DEBUG - 2012-01-18 05:23:53 --&gt; File loaded: application/views/bookSlot.php
DEBUG - 2012-01-18 05:23:53 --&gt; File loaded: application/views/templateVolunteer.php
DEBUG - 2012-01-18 05:23:53 --&gt; Final output sent to browser
DEBUG - 2012-01-18 05:23:53 --&gt; Total execution time: 20.9332
DEBUG - 2012-01-18 05:25:09 --&gt; Config Class Initialized
DEBUG - 2012-01-18 05:25:09 --&gt; Hooks Class Initialized
DEBUG - 2012-01-18 05:25:09 --&gt; Utf8 Class Initialized
DEBUG - 2012-01-18 05:25:09 --&gt; UTF-8 Support Enabled
DEBUG - 2012-01-18 05:25:09 --&gt; URI Class Initialized
DEBUG - 2012-01-18 05:25:09 --&gt; Router Class Initialized
DEBUG - 2012-01-18 05:25:09 --&gt; Output Class Initialized
DEBUG - 2012-01-18 05:25:09 --&gt; Security Class Initialized
DEBUG - 2012-01-18 05:25:09 --&gt; Input Class Initialized
DEBUG - 2012-01-18 05:25:09 --&gt; Global POST and COOKIE data sanitized
DEBUG - 2012-01-18 05:25:09 --&gt; Language Class Initialized
DEBUG - 2012-01-18 05:25:09 --&gt; Loader Class Initialized
DEBUG - 2012-01-18 05:25:09 --&gt; Helper loaded: url_helper
DEBUG - 2012-01-18 05:25:09 --&gt; Helper loaded: form_helper
DEBUG - 2012-01-18 05:25:09 --&gt; Database Driver Class Initialized
DEBUG - 2012-01-18 05:25:10 --&gt; Session Class Initialized
DEBUG - 2012-01-18 05:25:10 --&gt; Helper loaded: string_helper
DEBUG - 2012-01-18 05:25:10 --&gt; Session routines successfully run
DEBUG - 2012-01-18 05:25:10 --&gt; XML-RPC Class Initialized
DEBUG - 2012-01-18 05:25:10 --&gt; DX Auth Initialized
DEBUG - 2012-01-18 05:25:10 --&gt; Session class already loaded. Second attempt ignored.
DEBUG - 2012-01-18 05:25:10 --&gt; Config file loaded: application/config/dx_auth.php
DEBUG - 2012-01-18 05:25:10 --&gt; Language file loaded: language/english/dx_auth_lang.php
DEBUG - 2012-01-18 05:25:10 --&gt; Form Validation Class Initialized
DEBUG - 2012-01-18 05:25:10 --&gt; Controller Class Initialized
DEBUG - 2012-01-18 05:25:10 --&gt; Model Class Initialized
DEBUG - 2012-01-18 05:25:10 --&gt; Model Class Initialized
DEBUG - 2012-01-18 05:25:10 --&gt; Session class already loaded. Second attempt ignored.
DEBUG - 2012-01-18 05:25:15 --&gt; File loaded: application/views/bookSlot.php
DEBUG - 2012-01-18 05:25:15 --&gt; File loaded: application/views/templateVolunteer.php
DEBUG - 2012-01-18 05:25:15 --&gt; Final output sent to browser
DEBUG - 2012-01-18 05:25:15 --&gt; Total execution time: 6.1827
#3

[eluser]Matt S.[/eluser]
Funny how something so simple could cause so much confusion...you forgot to add an echo before set_radio()!

Code:
&lt;?php echo set_radio('volDate', $val);?&gt;/>
#4

[eluser]iamzvonko[/eluser]
Oh man, just shoot me now!

Thanks
#5

[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:
&lt;?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');
  }
}
}
?&gt;

FORM VIEW
Code:
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;My Form&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;?php echo validation_errors(); ?&gt;
&lt;?php echo form_open('form'); ?&gt;
<h5>Email Address</h5>
&lt;input type="text" name="email" value="&lt;?php echo set_value('email'); ?&gt;" size="50" /&gt;
<h5>Explicit</h5>
&lt;input type="radio" value="Mon" name="volDate" id="volDate" &lt;?php echo set_radio('volDate', "Mon");?&gt;/&gt;Monday&nbsp;&nbsp;
&lt;input type="radio" value="Tue" name="volDate" id="volDate" &lt;?php echo set_radio('volDate', "Tue");?&gt;/&gt;Tuesday&nbsp;&nbsp;
&lt;input type="radio" value="Wed" name="volDate" id="volDate" &lt;?php echo set_radio('volDate', "Wed");?&gt;/&gt;Wednesday&nbsp;&nbsp;
&lt;input type="radio" value="Thu" name="volDate" id="volDate" &lt;?php echo set_radio('volDate', "Thu");?&gt;/&gt;Thurday&nbsp;&nbsp;
&lt;input type="radio" value="Fri" name="volDate" id="volDate" &lt;?php echo set_radio('volDate', "Fri");?&gt;/&gt;Friday&nbsp;&nbsp;
<h5>With Loop</h5>
&lt;?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'];
?&gt;
&lt;input type="radio" value="&lt;?php echo $val2;?&gt;" name="volDate2" id="volDate2"&lt;?php echo set_radio('volDate2', $val2);?&gt;/&gt;&lt;?php echo $day2['day']; ?&gt;&nbsp;&nbsp;
&lt;?php } ?&gt;
<div>&lt;input type="submit" value="Submit" /&gt;&lt;/div>
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;

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:
...
&lt;?php
echo "<b>Step 1: Select a date</b><br />&nbsp;&nbsp;";
foreach ($days as $day) {
  $val = $day['dateID'];
?&gt;
&lt;input type="radio" value="&lt;?php echo $val;?&gt;" name="volDate" id="volDate"&lt;?php echo set_radio('volDate', $val);?&gt;/&gt;&lt;?php echo $day['day']; ?&gt;&nbsp;&nbsp;
&lt;?php } ?&gt;
<br>
&lt;?php
echo "<br /><b>Step 2: Select a location</b><br />&nbsp;&nbsp;";
foreach ($sites as $site) {
  $val = $site['siteID'];
  echo '&lt;input type="radio" value="'.$val.'" name="volSite" id="volSite"&gt;'.$site['name'].set_radio('volSite', $val).'&nbsp;&nbsp;';
}
?&gt;
...
#6

[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;
}
#7

[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?




Theme © iAndrew 2016 - Forum software by © MyBB