douan3414 2015-06-13 19:01
浏览 49
已采纳

在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.

  • 写回答

1条回答 默认 最新

  • duanbo1659 2015-06-13 19:01
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 深度学习残差模块模型
  • ¥20 两个不同Subnet的点对点连接
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用
  • ¥15 请帮我看一看数电项目如何设计