简体   繁体   English

在CodeIgniter 2.2.0中,为什么表单验证器不对数组输入数据调用我的自定义验证例程?

[英]In CodeIgniter 2.2.0, why does the form validator not invoke my custom validation routine on array input data?

Here's the situation: While building a project in CodeIgniter 2.2.0, I was attempting to validate a tabular form with a custom data validator. 情况如下:在CodeIgniter 2.2.0中构建项目时,我试图使用自定义数据验证器来验证表格形式。 The tabular form was set up to transmit POST data to the server in standard array format, and I was using non-zero based numeric keys. 表格格式设置为以标准阵列格式将POST数据传输到服务器,而我使用的是基于非零的数字键。 My keys, which encode important data, happened to start with 1 on the form I was attempting to debug. 我的用于编码重要数据的键恰巧以我尝试调试的形式以1开头。 I noticed that CodeIgniter's Form_validation class was invoking my validator on the 2nd and 3rd row of my data (with keys 2 and 3, respectively) but not the first row (with key 1). 我注意到CodeIgniter的Form_validation类在数据的第二行和第三行(分别具有键2和3)上调用了我的验证器,但没有调用第一行(具有键1)。

Why should this be so? 为什么会这样呢? The CodeIgniter Form_validation documentation indicates that you can use non-numeric array keys, so one would expect non-zero numeric array keys to work too. CodeIgniter Form_validation文档指示您可以使用非数字数组键,因此人们希望非零数字数组键也能正常工作。

After some debugging, I found that Form_validation::_execute() (in system/libraries/Form_validation.php) has a $cycles variable, a zero-based integer, that measures the number of times any particular rule has been invoked on array-based input. 经过一些调试后,我发现Form_validation :: _ execute()(在system / libraries / Form_validation.php中)有一个$ cycles变量,它是一个从零开始的整数,它测量在数组上调用任何特定规则的次数。基于输入。 Unfortunately, the __execute() function uses this $cycles variable to reference elements of the post data (eg, line 552 in my build, which appears to be 2.2.0). 不幸的是,__ execute()函数使用此$ cycles变量来引用发布数据的元素(例如,我的版本中的第552行,似乎是2.2.0)。 This has the result of bypassing input array elements that are not keyed to zero-based integers. 这样的结果是绕过了没有键入到从零开始的整数的输入数组元素。 I solved my problem by changing my array keying scheme to be a zero-based count, and encoding elsewhere the data I had been putting in those keys. 我通过将数组键控方案更改为从零开始的计数并在其他位置编码已放入这些键中的数据来解决了我的问题。

Perhaps this will be fixed in CodeIgniter 3, but for those out there still using CodeIgniter 2 and validating array input from forms using the default validation tools, watch out for this. 也许这将在CodeIgniter 3中修复,但是对于那些仍在使用CodeIgniter 2并使用默认验证工具验证来自表单的数组输入的用户,请注意这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM