Welcome Guest, Not a member yet? Register   Sign In
set_radio problem
#1

[eluser]Zeff[/eluser]
Hi all,

Here is a simple code snippet for testing the set_radio method from the form helper:
In my controller:
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Start extends CI_Controller {

public function __construct()  {
    parent::__construct();
    $this->load->helper('form');
}

public function setval() {
    $this->load->library('form_validation');
    $this->form_validation->set_rules('result', 'Result', 'required');
    
    $data['result'] = '11';
    
    if ($this->form_validation->run() == FALSE)
    {
        $this->load->view('setval', $data, false);
    }else{
        echo '<p>Ok...</p>';
    }
}
}

The view contains:
Code:
&lt;?php # test set_radio

echo '<div class="error">'.validation_errors().'</div>';

echo '<p>Received '.$result.' as value for $result</p>';

echo form_open('/start/setval/');      

echo '<p>';
    $attrs = array('name' => 'result', 'id' => 'id_resultval_1', 'value'=> '1', set_radio('result', '1'), 'class' => 'inputRadio');
    echo form_radio($attrs);
    echo form_label('Result 1', 'id_resultval_1');
echo '</p>';
            
echo '<p>';
    $attrs = array('name' => 'result', 'id' => 'id_resultval_11', 'value'=> '11', set_radio('result', '11'), 'class' => 'inputRadio');
    echo form_radio($attrs);
    echo form_label('Result 11', 'id_resultval_11');
echo '</p>';
?&gt;

<div class="submit">
&lt;input name="submit" class="inputButton" type="submit" id="btnCancel" value="Go" /&gt;
&lt;input name="submit" class="inputButton" type="submit" id="btnSave" value="Cancel" /&gt;
</div>

&lt;?php
echo form_close();
?&gt;
At first load, I expected that the second radio should be checked but although the $result var is '11', none of the radio buttons is checked...

I can't find what I'm doing wrong here, please help!

Thanks!
#2

[eluser]TheFuzzy0ne[/eluser]
I'm not entirely sure how you're supposed to do it when you're using an array to pass the parameters to the form_radio() function, but the code below might work.

Code:
&lt;?php # test set_radio

echo '<div class="error">'.validation_errors().'</div>';

echo '<p>Received '.$result.' as value for $result</p>';

echo form_open('/start/setval/');      

echo '<p>';
    $attrs = array('name' => 'result', 'id' => 'id_resultval_1', 'value'=> '1', 'checked' => (set_radio('result', '1') != ''), 'class' => 'inputRadio');
    echo form_radio($attrs);
    echo form_label('Result 1', 'id_resultval_1');
echo '</p>';
            
echo '<p>';
    $attrs = array('name' => 'result', 'id' => 'id_resultval_11', 'value'=> '11', 'checked' => (set_radio('result', '11') != ''), 'class' => 'inputRadio');
    echo form_radio($attrs);
    echo form_label('Result 11', 'id_resultval_11');
echo '</p>';
?&gt;

<div class="submit">
&lt;input name="submit" class="inputButton" type="submit" id="btnCancel" value="Go" /&gt;
&lt;input name="submit" class="inputButton" type="submit" id="btnSave" value="Cancel" /&gt;
</div>

&lt;?php
echo form_close();
?&gt;

form_radio() expected the array to contain an index called "checked" and for the value to be either TRUE or FALSE. You weren't passing it at all in your array, and in any case, set_radio() returns either an empty string, or ' checked="checked"', not a boolean as you might expect.

set_radio() also takes an optional third parameter, which is used as the default. It's up to you to come up with the condition that returns TRUE or FALSE according to your defaults.

Hope this helps.
#3

[eluser]TheFuzzy0ne[/eluser]
Something that just occured to me... This may also work, and may make your code a bit more readable.

Code:
$attrs = array('name' => 'result', 'id' => 'id_resultval_1', 'class' => 'inputRadio');
    echo form_radio($attrs, '1', set_radio('result', '1'));

Lets assume you have the value of the checkbox stored in your database as an integer (0=unchecked, 1=checked), and the column was called my_checkbox. You can pull the data out of the database, generate an array of default values based on that. You could then do the following to set the default value:
Code:
$attrs = array('name' => 'result', 'id' => 'id_resultval_1', 'class' => 'inputRadio');
    echo form_radio($attrs, '1', set_radio('result', '1', ($defaults['my_checkbox'] == '1'));
#4

[eluser]Zeff[/eluser]
Hi TheFuzzyOne,

Thanks for your replies! I tried what you suggested, but it doesn't work... But, your snips put me on the right track: I examined the form_validation library and found (I should have known...) that set_radio(a,b,bool) method does not return TRUE or FALSE, but a string 'checked="checked"':

Code:
// library: /system/libraries/form_validation.php
/**
  * Set Radio
  *
  * Enables radio buttons to be set to the value the user
  * selected in the event of an error
  *
  * @access public
  * @param string
  * @param string
  * @return string
  */
public function set_radio($field = '', $value = '', $default = FALSE)
{
  if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata']))
  {
   if ($default === TRUE AND count($this->_field_data) === 0)
   {
    return ' checked="checked"';
   }
   return '';
  }

  $field = $this->_field_data[$field]['postdata'];

  if (is_array($field))
  {
   if ( ! in_array($value, $field))
   {
    return '';
   }
  }
  else
  {
   if (($field == '' OR $value == '') OR ($field != $value))
   {
    return '';
   }
  }

  return ' checked="checked"';
}

Since the set_radio() method compares to POSTED data to do what it's written for, the $result variable can not be used to set. So the 'default value' (third) parameter in form_radio is the one to be set (see your last reply). I solved it this way:

Code:
echo '<p>';
    $attrs = array('name' => 'result', 'id' => 'id_resultval_11', 'class' => 'inputRadio');
    echo form_radio($attrs, '11', ($result==11)?TRUE:FALSE);
    echo form_label('Result 11', 'id_resultval_11');
echo '</p>';
echo '<p>';
    $attrs = array('name' => 'result', 'id' => 'id_resultval_1', 'class' => 'inputRadio');
    echo form_radio($attrs, '1', ($result==1)?TRUE:FALSE);
    echo form_label('Result 1', 'id_resultval_1');
echo '</p>';

Thanks for the assist!
#5

[eluser]Aken[/eluser]
FYI, you don't need the ternary if/else there. The comparison itself will return true or false.

Code:
echo form_radio($attrs, '11', ($result == 11));




Theme © iAndrew 2016 - Forum software by © MyBB