CodeIgniter Forums
form_radio() validation problem - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: form_radio() validation problem (/showthread.php?tid=45737)



form_radio() validation problem - El Forum - 10-04-2011

[eluser]Unknown[/eluser]
I want to achieve 2 things with the radio buttons:

1) I want to know how to do validation for radio buttons.

2) Upon validation error, how to auto-select the last selected radio button.

Below is the code I have. I know I haven't done it correctly. If someone could guide me to the right direction, that'll be great. Thanks

In my view:
Code:
$form_data['radio1'] = array(
  'name'     => 'radio_select',
  'id'      => 'radio1',
  'value'    => 'radio1',
  'style'    => 'margin-bottom:12px; margin-right:4px;',
  'checked'  => set_value('true', isset($radio_select['radio1']['checked'])?TRUE:FALSE)
);

$form_data['radio2'] = array(
  'name'     => 'radio_select',
  'id'      => 'radio2',
  'value'    => 'radio2',
  'style'    => 'margin-bottom:12px; margin-right:4px;',
  'checked'  => set_value('false', isset($radio_select['radio2']['checked'])?TRUE:FALSE)
);

echo form_radio($form_data['radio1']); echo "Radio 1";
echo form_radio($form_data['radio2']);  echo "Radio 2";

In my controller:
Code:
$this->form_validation->set_rules('radio_select', 'Radio button', 'required');