Welcome Guest, Not a member yet? Register   Sign In
Arrayed Inputs Validation
#2

I've submitted a pull request for this. Looking into the code of how validated data is retrieved I see that Codeigniter4/system/Validation/DotArrayFilter.php makes use of the array_merge_recursive function. The PHP manual states that 
Quote:If the input arrays have the same string keys, then the values for these keys are merged together into an array, and this is done recursively, so that if one of the values is an array itself, the function will merge it with a corresponding entry in another array too. If, however, the arrays have the same numeric key, the later value will not overwrite the original value, but will be appended.
It seems that since I request '.2' before '.0' and '.1' the values of '.0' and '.1' are appended to the returned merged array, which is why the somename array has keys of 2, 3, and 4. I'm of the opinion this is a bug. If I request an array with strings there won't be a problem. For instance 'somename.first' would return the expected value even if it was preceded by 'somename.last'. However, if I ask for 'somename.1' before 'somename.0' I'll be retrieving a different index in my resultant data. This doesn't seem like a big deal, but if I have position dependent insertions (as is the case with what I'm attempting to do) happening into a database after form submission it can produce an error. The workarounds are either prepend your numerical indices with an arbitrary letter for instance, 'somename.a1' or to sort the $rules beforehand. However, sorting the rules doesn't work if for instance your input fields are 'somename.0', 'somename.1' and 'somename.3', the resultant array will be indexed 0,1,2 rather than 0,1,3. array_merge_recursive doesn't seem to respect numerical indices at all other than starting the resultant merge with whatever the first index is.
Reply


Messages In This Thread
Arrayed Inputs Validation - by grimpirate - 01-14-2024, 10:11 AM
RE: Arrayed Inputs Validation - by grimpirate - 01-15-2024, 01:25 PM



Theme © iAndrew 2016 - Forum software by © MyBB