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 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动