开发者

setting form validation rules for form_radio()

Hello I want to have a form_radio() set as obligatory field in codeigniter any ideas on how can I do that?

this is my form:

           echo for开发者_如何学编程m_radio('radio','earing',FALSE)."earings";
           echo form_radio('radio','bag',FALSE)."bag";
           echo form_radio('radio','bracelet',FALSE)."bracelet";
           echo form_close();

of course only only one one of them needs to be filed each time...

thanks in advance!


You need to add validation to the form's submit event. see example below:

<form action="page.php" method="post" onsubmit="return isValid(this);">
    <fieldset>
        <legend>Radio Buttons</legend>

        <input type="radio" value="1" name="myRadio" />Selection 1<br />
        <input type="radio" value="2" name="myRadio" />Selection 2<br />
        <input type="radio" value="3" name="myRadio" />Selection 3<br />
    </fieldset>

    <input type="submit" name="submitit" value="Submit" />
    <input type="reset" name="reset" value="Clear" />
</form>

<script type="text/javascript">

    function isValid(frm) {
        //  Initialize return value
        var isChecked = false;

        //  Check if radio was selected
        for (var i=frm.myRadio.length-1; i > -1; i--) {
            if (frm.myRadio[i].checked) {
                isChecked = true;
                break;
            }
        }

        if (isChecked == false) {
            alert("You must select a radio button");
            //  Data is invlaid,
            return false;
        }

        //  Everything is ok, continue with form submit
        return true;
    }
</script>


One way to avoid needing to validate the radio buttons is to simply have one checked by default. This way you don't even have to worry if the user hasn't selected a radio button since once one is checked there will always be one checked.

This doesn't answer the specific question on how to validate forms in CodeIgnitor (which unfortunately I do not know the answer to), but it may prove useful to you in accomplishing your overall goal.


You code needs to be submited to controller if you want to use validation.

class Form extends Controller {

    function index()
    {
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');

        $this->form_validation->set_rules('radio', 'Product', 'required');

        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('myform');
        }
        else
        {
            $this->load->view('formsuccess');
        }
    }
}

By you dont really need validation for RADIO. When you generate the radio button, make one selected and it will always have value.

Try it like this, this should work better. It will generate the radio boxes with 1st one checked by default if value for radios is not set:

echo form_radio('radio','earing', set_radio('radio', 'earing', TRUE))."earings";
echo form_radio('radio','bag', set_radio('radio', 'bag'))."bag";
echo form_radio('radio','bracelet', set_radio('radio', 'bracelet'))."bracelet";
echo form_close();


The way I have solved it so far (and it seems ok to me)...is by a simple if statement in my controller like that:

 if($this->input->post('upload') && $this->input->post('radio')){
             $this->Gallery_model->do_upload();
          }

but I was wondering if there is a more proper way by using the form validation class so I won't mark this as an answer...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜