codeIgniter 2.0 - 验证阵列 [英] CodeIgniter 2.0 - validating arrays

查看:153
本文介绍了codeIgniter 2.0 - 验证阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些问题了一块表单验证code的转换为CI 2.0。我试图验证复选框数组但由于某种原因验证失败运行回调或不验证。

I'm having some problems converting a piece of form validation code to CI 2.0. I'm trying to validate an array of checkboxes but for some reason validation fails to run the callback or doesn't validate.

我如何可以验证复选框数组,这样至少有一个被选中,值必须是其中的一个选项(期权数组的键)?

How can I validate an array of checkboxes so that at least one is checked and values must be one of the options (key of an options array)?

编辑:

下面是它失败我一个更好的解释。可以说,我有以下字段:

Here is a better explanation of where it is failing me. Lets say I has these fields:

<input type="checkbox" value="1" name="purpose[]" />
<input type="checkbox" value="2" name="purpose[]" />

我已经设置两个规则的目的[] - 一个是需要规则的,另一种是定制的回调,检查该值是present可能值的数组

I've set two rules for purpose[] - one is the required rule, the other is a custom callback that checks that the value is present in a array of possible values.

如果我编辑的字段的名称为:

If I edit the name of the field to:

<input type="checkbox" value="1" name="purpose[abc]" />

甚至改变为,验证通过的值。它忽略了所需的规则,我的自定义的回调。

or even change the value to "", the validation passes. It ignores the required rule and my custom callback.

任何人对如何处理这个想法?

Anybody have an idea on how to deal with this?

推荐答案

这是数组必须被传递给你打电话set_rules后缀为开/关方括号,就像这样:

An array must be passed to your set_rules call suffixed with opening / closing square brackets, like so:

$this->form_validation->set_rules('checkboxes[]', 'My Checkboxes', 'required');

有CI的用户手册中的详细信息 - 的http://ellislab.com/$c$cigniter/user_guide/libraries/form_validation.html#arraysasfields

There are more details in the CI user guide - http://ellislab.com/codeigniter/user_guide/libraries/form_validation.html#arraysasfields

要处理您的复选框值的检查是几个值之一,你需要创建一个自定义的回调函数 - 的http://ellislab.com/$c$cigniter/user_guide/libraries/form_validation.html#callbacks

To handle the check for your checkbox values to be one of several values, you would need to create a custom callback function - http://ellislab.com/codeigniter/user_guide/libraries/form_validation.html#callbacks

这篇关于codeIgniter 2.0 - 验证阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆