Welcome Guest, Not a member yet? Register   Sign In
How to validate radio button ?
#1

[eluser]runrun[/eluser]
Hi,

I can make every other thing validated except the radio button.

view:
Quote: <?=validation_errors()?>
<form id="sell_form" method="post">
<div class="left_col float_l">
&lt;input type="radio" value="1" name="sell_option" id="classification" /&gt;
<label for="classification">classification</label>
</div>
<div class="right_col float_l">
&lt;input type="radio" value="2" name="sell_option" id="auction" /&gt;
<label for="auction">auction</label>
</div>
<div class="clear_float center">&lt;input type="submit" value="GO" /&gt;&lt;/div>
&lt;/form&gt;

controller:
Code:
class Sell extends Controller
{
    function index()
    {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('sell_option', 'Sell option', 'required');
        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('sell_view');
        }
        else
        {
            redirect('sell/sell_step1');
        }
    }
}

The error message never shows even though the form is not validated.
#2

[eluser]zutis[/eluser]
At a guess I would suggest that for some reason the sell_option is not empty even if not selected.

Have you tried a different rule maybe ... so instead of 'required' try 'numeric'?
#3

[eluser]runrun[/eluser]
Thanks, I know why. its because the floating elements, they over float the message.
#4

[eluser]zutis[/eluser]
LOL :OP
#5

[eluser]runrun[/eluser]
Wait, I believe I've just found a bug.

The error message won't show if you validate radio button singlely. The form validation class only if your form has more than rule set. Compare the code below and the original code.

Code:
&lt;?=validation_errors()?&gt;
      &lt;form id=“sell_form” method=“post”&gt;
        <div class=“left_col float_l”>
          &lt;input type=“radio” value=“1” name=“sell_option” id=“classification” /&gt;
          <label for=“classification”>classification</label>
        </div>
        <div class=“right_col float_l”>
          &lt;input type=“radio” value=“2” name=“sell_option” id=“auction” /&gt;
          <label for=“auction”>auction</label>
        </div>
        &lt;input type="text" name="id" /&gt;
        <div class=“clear_float center”>&lt;input type=“submit” value=“GO” /&gt;&lt;/div>
      &lt;/form&gt;

Quote:class Sell extends Controller
{
function index()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('sell_option', 'Sell option', 'required');
$this->form_validation->set_rules('id', 'ID', 'required' );
if ($this->form_validation->run() == FALSE)
{
$this->load->view('sell_view');
}
else
{
redirect('sell/sell_step1');
}
}
}

In the view I added an input field with name 'id'. In the controller i added rule set for the new added input. Now the error messages will shows up if the form is not valid.




Theme © iAndrew 2016 - Forum software by © MyBB