I've tried to search this but I found no luck.
It says that $request->data will be deprecated in 4.0 and suggesting to use $request->getData() instead.
How do I unset request data when we don't have the option to unset it?
This is useful when logging in or registering with password. When they failed the validation, they have to retype the password.
There's a simple way to do that without even touching the request object, and that is by passing an empty string to the controls value option, that way the field will always be empty when it's being rendered:
$this->Form->control('password', ['value' => ''])
That being said, if there really is the need to unset POST data on the request object (which often times indicates that there's something wrong with what you're doing), you can use withParsedBody() to either empty everything:
$this->request = $this->request->withParsedBody([]);
or write back a partial array:
$data = $this->request->getData();
unset($data['password']);
$this->request = $this->request->withParsedBody($data);
This may look a little weird, but as already mentioned, the need to unset data on a request object often times indicates a flaw in your application logic.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With