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

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

问题描述:

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. 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. My keys, which encode important data, happened to start with 1 on the form I was attempting to debug. 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).

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 2.2.0中构建项目时,我试图用表格形式验证表格形式 自定义数据验证器。 表格形式设置为以标准数组格式将POST数据传输到服务器,我使用非零的数字键。 我的密钥编码重要数据,恰好在我试图调试的表单上以1开头。 我注意到CodeIgniter的Form_validation类在我的数据的第2行和第3行(分别使用键2和3)调用我的验证器,而不是第一行(使用键1)。 p>

为什么会这样? CodeIgniter Form_validation文档表明您可以使用非数字数组 键,所以人们会期望非零数字数组键也可以工作。 p> div>

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. Unfortunately, the __execute() function uses this $cycles variable to reference elements of the post data (e.g., line 552 in my build, which appears to be 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.