Welcome Guest, Not a member yet? Register   Sign In
SOLVED - Help with form validation radio buttons
#1

[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">&lt;input name="application[require_option1]" type="radio" value="yes" &lt;?php echo isset($form['require_option1']) && $form['require_option1'] == 'yes' ? ' checked="checked"' : '' ?&gt; /&gt; Yes</label>
    <label class="radio">&lt;input name="application[require_option1]" type="radio" value="no" &lt;?php echo isset($form['require_option1']) && $form['require_option1'] == 'no' ? ' checked="checked"' : '' ?&gt;  /&gt; No</label>
  
    <label for="textfield">If yes, please give details:</label>
    &lt;textarea name="application[require_textfield1]" cols="35" rows="4" id="require_textfield1"&gt;&lt;?php echo !isset($form['require_textfield1']) ? '' : $form['require_textfield1'] ?&gt;&lt;/textarea&gt;

Help is greatly appreciated.

cheers
grafxflow
#2

[eluser]Unknown[/eluser]
Problem has been sorted.
#3

[eluser]deegeedee[/eluser]
So what was the answer?
#4

[eluser]MathBoon[/eluser]
The answer would be nice, having kind of the same issue here.
#5

[eluser]deegeedee[/eluser]
Would have been nice had the answer been posted. I'm just getting ready to tackle this problem. What's posted above seems plausible.




Theme © iAndrew 2016 - Forum software by © MyBB