Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Unset Request Data in CakePHP 3.4

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.

like image 351
Patrick Avatar asked Apr 07 '26 22:04

Patrick


1 Answers

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.

like image 142
ndm Avatar answered Apr 22 '26 09:04

ndm