• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
form_radio() + set_radio() == true?


I'm trying to create a form with radio buttons and make it keep the different options checked if validation fails...

<?php echo '<input type="radio" name="answer'.$item->id.'" value="option1" '.set_radio('answer'.$item->id.'', 'option1').' /> '.$item->answer1.''?>

That works... but is it possible to somehow combine form_radio and set_radio?

I don't think set_radio can be used with form_radio as far as I can tell..

I'm a little out of practice with radio buttons I must admit, but I think that they are similar in function to checkboxes. Here is my solution dealing with checkboxes which may give you some idea on how to proceed if it's possible:

<?php echo form_checkbox('pa_surface', '1', $project->pa_surface) ?> Surface

The first is the name of the form 'function', secon the value that is assigned, and third is the variable coming from the database through the controller and model code.

I can't see a radio button working in much a different way.

Hey Warz,

it's a little late, but i have/had the same problem and after looking at the helper-function form_radio(), i found a really easy way to combine those two:


The fourth parameter is called "extra" and it's only being added to the returned html, and since set_radio only returns "checked='checked'" OR nothing, it fits perfectly.

Still, if you use an array with form_radio like this:

$data = array("name" => $name, "value" => $value);

then you'll probably need to use it like this, because the function doesn't catch any "extra" field of an array:

$data = array("name" => $name, "value" => $value);

It'd be nice if they could built in a catch for an attribute like "PureHTML", which could by used just like the "extra" parameter.

I just successfully used them in combination like this:

form_radio(array("name"=>"newuser","id"=>"newuser","value"=>"1", 'checked'=>set_radio('newuser', '1', TRUE)));

I struggled with this for some time until I realized that you can't use set_radio() if you have not used set_rules() on the radio buttons that you are trying to set. So if the name attribute on your radio buttons are "myradio" you will need to use the set_rules() function for "myradio". You can either set it to required or if you really don't want any kind of validation on it, you can only pass the name to the set_rules() function and it will work. If you don't want validation on it, the function call would be


and if you want them to be required, you would add

<code>$this->form_validation->set_rules("myradio", "<HUMAN FRIENDLY NAME>", "required");</code>

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.