codeigniter表单验证和数组作为字段名称

时间:2021-12-11 07:39:25

I have a set of checkboxes in my HTML that look like this,

我的HTML中有一组复选框,如下所示,

div class="grid_7">
                    <fieldset class="age shadow_50">
                        <label class="legend">Age Group</label>
                            <div class="formRow checkbox">
    <input id="" type="checkbox" name="age[]" value="child" />
    <label>Child</label>
</div>

                            <div class="formRow checkbox">
    <input id="" type="checkbox" name="age[]" value="30's" />
    <label>30s</label>
</div>
                            <div class="formRow checkbox">
    <input id="" type="checkbox" name="age[]" value="60's" />
    <label>60's</label>
</div>

                            <div class="formRow checkbox">
    <input id="" type="checkbox" name="age[]" value="teen" />
    <label>Teen</label>
</div>
                            <div class="formRow checkbox">
    <input id="" type="checkbox" name="age[]" value="40's" />
    <label>40's</label>
</div>

                            <div class="formRow checkbox">
    <input id="" type="checkbox" name="age[]" value="70's" />
    <label>70's</label>
</div>
                            <div class="formRow checkbox">
    <input id="" type="checkbox" name="age[]" value="20's" />
    <label>20's</label>
</div>

                            <div class="formRow checkbox">
    <input id="" type="checkbox" name="age[]" value="50's" />
    <label>50's</label>
</div>
                    </fieldset>
                </div>

I have set a validation rule in my controller that (in my mind) makes sure that a checkbox has been selected,

我在我的控制器中设置了一个验证规则(在我看来)确保选中了一个复选框,

$this->form_validation->set_rules('age[]', 'age group', 'required|trim');

When testing this however I get an error message for ever checkbox with name age[] I just want to check that age[] is not empty.

然而,当我测试这个时,我收到一个错误消息,永远是名称年龄的复选框[]我只想检查年龄[]是不是空的。

How can I achieve this?

我怎样才能做到这一点?

2 个解决方案

#1


1  

You cannot test age[] like that, it is an array. There are a few ways to do this but what you did required is not one of them.

你无法测试age [],它是一个数组。有几种方法可以做到这一点,但你所要求的不是其中之一。

You could use javascript or by running the age[] value through your own custom callback function is one method.

你可以使用javascript或通过你自己的自定义回调函数运行age []值是一种方法。

Details for using arrays in CI are here:
https://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#arraysasfields

在CI中使用数组的详细信息如下:https://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#arraysasfields

If you do it the javascript route, you can use jQuery to iterate through your check boxes (use a class to link them) and just make sure that 1 of them is checked.

如果你使用javascript路由,你可以使用jQuery迭代你的复选框(使用一个类来链接它们),并确保检查其中一个。

#2


0  

Old question but for anyone struggling to do this I believe you can do this by setting a rule on age rather than age[], i.e.

老问题,但对于任何努力做到这一点的人,我相信你可以通过设定年龄而不是年龄[]的规则来做到这一点,即

$this->form_validation->set_rules('age', 'age group', 'fnName');

#1


1  

You cannot test age[] like that, it is an array. There are a few ways to do this but what you did required is not one of them.

你无法测试age [],它是一个数组。有几种方法可以做到这一点,但你所要求的不是其中之一。

You could use javascript or by running the age[] value through your own custom callback function is one method.

你可以使用javascript或通过你自己的自定义回调函数运行age []值是一种方法。

Details for using arrays in CI are here:
https://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#arraysasfields

在CI中使用数组的详细信息如下:https://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#arraysasfields

If you do it the javascript route, you can use jQuery to iterate through your check boxes (use a class to link them) and just make sure that 1 of them is checked.

如果你使用javascript路由,你可以使用jQuery迭代你的复选框(使用一个类来链接它们),并确保检查其中一个。

#2


0  

Old question but for anyone struggling to do this I believe you can do this by setting a rule on age rather than age[], i.e.

老问题,但对于任何努力做到这一点的人,我相信你可以通过设定年龄而不是年龄[]的规则来做到这一点,即

$this->form_validation->set_rules('age', 'age group', 'fnName');