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

142 views Asked by At

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

There are 1 answers

0
Ben Birney On BEST ANSWER

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.