Welcome Guest, Not a member yet? Register   Sign In
Validate radio buttons.
#1

[eluser]insub2[/eluser]
I was wondering how to require that a radio be selected if there is no default value. Because in order for the Form Validation Class to check a field, it needs to be in the $_POST array. But if none of the radio buttons are selected, then no key will be inserted into the $_POST array.

From the view:
Code:
<?=form_radio(array(    'name'=>overall,
            'id'=>'overall-one',
            'value'=>'one',
            'checked'=>($returned=='one')? TRUE : FALSE
            )
);?>
<?=form_radio(array(    'name'=>overall,

            'id'=>'overall-two',
            'value'=>'one',
            'checked'=>($returned=='two')? TRUE : FALSE
            )
);?>

From the controller:
Code:
$this->form_validation->set_rules('overall','Overall Rating','required')

But if the form is submitted without either selected, I just get an error:
Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined index: overall

Filename: forms/rate.php

Line Number: 99

And the print_r($_POST) shows no 'overall':
Code:
Array
(
    [category] =>
    [review_title] =>
    [review_details] =>
    [submit] => Submit
)


What do I do?

p.s. I don't want to set a default value.
#2

[eluser]Future Webs[/eluser]
this thread may help

http://ellislab.com/forums/viewthread/97665/
#3

[eluser]insub2[/eluser]
BAH...
that error is for the $returned value not being set when there isn't a $_POST['overall']. Not from the validation.

That pointed me the right direction. Thanks dude.

Code:
<?=form_radio(array(    'name'=>overall,
            'id'=>'overall-one',
            'value'=>'one',
            'checked'=>(set_value(overall)=='one')? TRUE : FALSE
            )
);?>
<?=form_radio(array(    'name'=>overall,

            'id'=>'overall-two',
            'value'=>'one',
            'checked'=>(set_value(overall)=='two')? TRUE : FALSE
            )
);?>




Theme © iAndrew 2016 - Forum software by © MyBB