Welcome Guest, Not a member yet? Register   Sign In
form_radio helper function work-around for 'checked' attribute
#1

[eluser]TWP Marketing[/eluser]
This is tested in CI Version 2.1.0

Using the CI helper function form_radio, the 'checked' attribute does not work as I expect.
'checked' is a boolean value, according to html specs, and according to the form helper description in the User Guide, however if the value is set to FALSE, a browser (Firefox in this case, but also other Gecko based browsers) does not correctly interpret it.
I have not tested this using IE, Opera, Safari, etc.

Example code (which DOES NOT work correctly)

Code:
IN THE VIEW
...
$options_free = array(
'type'    => 'radio',
'name'    => 'ad_lifetime',
'id'      => 'ad_lifetime_free',
'value'   => $this->config->item('free_lifetime'), // for instance a value of 7
'checked' => TRUE,
);
echo form_input($options_free)."\n";

$options_paid = array(
'type'    => 'radio',
'name'    => 'ad_lifetime',
'id'      => 'ad_lifetime_paid',
'value'   => $this->config->item('paid_lifetime'), // for instance, a value of 30
'checked' => FALSE,
);
echo form_input($options_paid)."\n";
...

The above PHP code will produce this HTML code:
Code:
...
<input type="radio" name="ad_lifetime" value="7" id="ad_lifetime_free" 'checked'="1"  />
<input type="radio" name="ad_lifetime" value="30" id="ad_lifetime_paid" 'checked'=""  />
...
NOTICE THE SECOND 'checked' ATTRIBUTE. CI sets it to an empty string.
THE PROBLEM: The second input element SHOULD NOT have the 'checked'="" attribute. It is not parsed correctly by my browsers (Firefox 11.0, Konqueror and rekong).

---------------------------------
My work-around is the following:
IN THE CONTROLLER
Code:
...
// test for current selection and set 'check...' flags
$check_free = FALSE; // default
$check_paid = FALSE; // default
switch( $ad_lifetime )
{ // match $ad_lifetime to configuration values
case $this->config->item('free_lifetime'): // for instance, a value of 7
  $check_free = TRUE; // set a boolean flag
  break;

case $this->config->item('paid_lifetime'): // for instance, a value of 30
  $check_paid = TRUE: // set a boolean flag
  break;

default:
  $check_free = TRUE; // this is the default radio button selection
  break:
}
$view_data['check_free'] = $checked_free; // pass the flags to the view
$view_data['check_paid'] = $checked_paid; // pass the flags to the view

...

$this->load->view('someview', $view_data); // load the view file
...

IN THE VIEW
Code:
...
$options_free = array(
'type'  => 'radio',
'name'  => 'ad_lifetime',
'id'    => 'ad_lifetime_free',
'value' => $this->config->item('free_lifetime'),
);
if( $check_free ){ $options_free['checked'] = TRUE; } // conditionally add 'checked' array element
echo form_input($options_free)."\n";

$options_paid = array(
'type'  => 'radio',
'name'  => 'ad_lifetime',
'id'    => 'ad_lifetime_paid_1',
'value' => $this->config->item('paid_lifetime_1'),
);
if( $check_paid ){ $options_paid['checked'] = TRUE; } // conditionally add 'checked' array element
echo form_input($options_paid)."\n";
...
Notice the conditional options array element 'checked' is written only when the radio button is selected.

My work-around produces the following html code, WHICH WORKS CORRECTLY in my browsers.
Code:
...
<input type="radio" name="ad_lifetime" value="7" id="ad_lifetime_free" 'checked'="1"  />
<input type="radio" name="ad_lifetime" value="30" id="ad_lifetime_paid"  />
...

Notice the second input element DOES NOT HAVE any 'checked' attribute.
This is the way I think the CI form_radio function SHOULD handle the boolean 'checked' option,
My work-around works "as expected" and CI ver 2.1.0 does not.

This can be written in a more condensed code, but this example shows the problem.

I'd like feedback on whether any of you have encountered this problem, and what you did to avoid it. Thanks




Theme © iAndrew 2016 - Forum software by © MyBB