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

#1
[eluser]Warz[/eluser]
Hello,

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

Code:
<?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?

#2
[eluser]Aidy[/eluser]
I don't think set_radio can be used with form_radio as far as I can tell..

#3
[eluser]mdcode[/eluser]
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:

Code:
<?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.

#4
[eluser]MathBoon[/eluser]
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:

Code:
form_radio($name,$value,$checkedOrNot,set_radio($name,$value));

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:

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

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

Code:
$data = array("name" => $name, "value" => $value);
form_radio($data,$value,$checkedOrNot,set_radio($name,$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.

#5
[eluser]virexmachina[/eluser]
I just successfully used them in combination like this:

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

#6
[eluser]Unknown[/eluser]
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

<code>$this->form_validation->set_rules("myradio");</code>

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.