-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug: getPost() still returns empty strings despite updating the $_POST superglobal via a filter #9456
Comments
|
Hello, However, I'm facing a challenge with input handling in my application. Some inputs might actually arrive as null, and if I automatically convert these null values to empty strings, it could lead to issues such as:
Could you please advise on the best approach to handle these cases? Specifically, I would like guidance on:
Also, is the recommended solution to handle these cases individually for each field, or use casts to automatically manage the conversion? Thanks for your help, |
Not in the context of a normal POST request. You may send JSON, but that is a different case. The If you are really determined to convert all empty strings to When it comes to inserting values into the database I would use casting, either on the model side (preferably) or in the entity. It all depends on the particular problem you're dealing with. Please feel free to post on our Forum, as we don't answer support questions here. |
Ok thank you for the support |
PHP Version
8.3
CodeIgniter4 Version
4.5.7
CodeIgniter4 Installation Method
Composer (using
codeigniter4/appstarter
)Which operating systems have you tested for this bug?
macOS
Which server did you use?
fpm-fcgi
Database
No response
What happened?
I am implementing a filter in CodeIgniter 4 to recursively convert empty strings in POST data to null. However, even though I update the $_POST superglobal and use the method setGlobal('post', $data) within the filter, subsequent calls to $this->request->getPost() continue to return the original values (with empty strings), ignoring the conversion.
Steps to Reproduce
Create a filter (for example, EmptyStringToNullFilter) that:
Retrieves POST data using $request->getPost().
Recursively converts empty strings to null.
Directly updates $_POST and calls $request->setGlobal('post', $data).
In the filter (or in the controller), perform a var_dump($this->request->getPost()) to verify the data.
Observe that the field that should be null (instead of an empty string) is not updated and remains an empty string.
Expected Output
The changes made to $_POST via the filter (specifically, the conversion of empty strings to null) should be reflected in subsequent calls to $this->request->getPost(), including updating the internal cache of the request.
Anything else?
No response
The text was updated successfully, but these errors were encountered: