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

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – codeigniter表单验证和数组作为字段名称脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我的 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迭代你的复选框(使用一个类来链接它们),并确保检查其中一个.

@H_301_38@

脚本宝典总结

以上是脚本宝典为你收集整理的php – codeigniter表单验证和数组作为字段名称全部内容,希望文章能够帮你解决php – codeigniter表单验证和数组作为字段名称所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。