Welcome Guest, Not a member yet? Register   Sign In
Should $request->getPost() and others modify data even when no filtering specified?
#1

I am developing an application which needs to handle a great number of forms.

These forms are large, and many of the fields are not mandatory.

One of the steps of processing these forms is converting all empty strings to null, in order to avoid having multiple "no-value" values stored in the db (such as '' and null).

The conversion is quite simple, but having to do this every time in the Controller (or elsewhere) is sort of annoying and error prone, so I thought I'd write up a CI4 Filter for the job.

Long story short, after I replace the empty strings with null and modify the $request via setGlobal('post', $modified_data), when I retrieve the data back inside the controller via $request->getPost() all nulls are converted back to string.

I believe the culprit is filter_var($value, $filter, $flags), which will convert nulls to empty string even if FILTER_UNSAFE_RAW (which according to php docs should let any value through).

There is an easy enough fix which is to test values for null before passing them through filter_var, and this seems to be restricted to the fetchGlobal method inside ResponseTrait.

Would this be an acceptable patch? I'm happy to provide a PR.

Eager to hear your thoughts,

André
Reply


Messages In This Thread
Should $request->getPost() and others modify data even when no filtering specified? - by andre.tannus - 04-01-2021, 02:39 PM



Theme © iAndrew 2016 - Forum software by © MyBB