[eluser]Unknown[/eluser]
Hi everyone,
I have a problem regarding a form validation.
On the form there are radio buttons with either 'yes' or 'no' values - either being required.
Code:
$this->form_validation->set_rules('application[require_option1]', 'Option1 Required', 'required');
Below that is a textfield which only needs to be required if the radio button 'yes' is selected.
Would using something like below to a second function be the best way to go?
Code:
$this->form_validation->set_rules('application[require_option1]', 'Option1 Required', 'required|callback_radiobutton');
// To check the Radio button is checked
function radiobutton($str)
{
if ($str == 'yes')
{
$this->form_validation->set_rules('application[require_textfield1]', 'textfield1 Required', 'required');
return FALSE;
}
else
{
return TRUE;
}
}
Here is the form itself
Code:
<label class="radio"><input name="application[require_option1]" type="radio" value="yes" <?php echo isset($form['require_option1']) && $form['require_option1'] == 'yes' ? ' checked="checked"' : '' ?> /> Yes</label>
<label class="radio"><input name="application[require_option1]" type="radio" value="no" <?php echo isset($form['require_option1']) && $form['require_option1'] == 'no' ? ' checked="checked"' : '' ?> /> No</label>
<label for="textfield">If yes, please give details:</label>
<textarea name="application[require_textfield1]" cols="35" rows="4" id="require_textfield1"><?php echo !isset($form['require_textfield1']) ? '' : $form['require_textfield1'] ?></textarea>
Help is greatly appreciated.
cheers
grafxflow