加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 22:32:15 所属栏目:PHP教程 来源:网络整理
导读:我的 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" / labelChild/label/div div class="f
我的 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>

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

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

然而,当我测试这个时,我收到一个错误消息,因为姓名为age []的复选框我只想检查age []是否为空.

我怎样才能做到这一点?

解决方法

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

您可以使用javascript或通过自己的自定义callback function运行age []值是一种方法.

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读