How to remove a single parameter from a symfony2 request object

I have the following request object and you want to remove 'email_suffix' from the controller before binding to the form. Is it possible?

public 'request' => object(Symfony\Component\HttpFoundation\ParameterBag)[8] protected 'parameters' => array 'registration' => array 'email' => string 's' (length=1) 'email_suffix' => string 'y.com' (length=5) 'password' => string '1234' (length=4) '_token' => string '967d99ba9f955aa67eb9eb004bd331151d816d06' (length=40) 'product_id' => string '2' (length=1) 'product_description' => string '12 month membership' (length=19) 'product_price' => string '6.99' (length=4) 

I tried $request->request->remove("registration[email_suffix]");

I can make a request $ request-> request-> remove ("registration") - this works.

I am currently doing this:

 $requestReg = $request->request->get('registration'); $requestReg['email'] = $requestReg['email'].'@'.$requestReg['email_suffix']; unset($requestReg['email_suffix']); $request->request->set('registration',$requestReg); 
+6
source share
2 answers

I'm not sure if your call to $request->request is a typo.

You should work with $request->attributes , which represents the ParameterBag class.

If you go through the methods in the ParameterBag , you will see that there is no way to disable the variable inside the array.

+1
source

It is possible to add and remove parameters from the request object in symfony2. You should look at the ComponeterBag Component , there such a method is called remove($key) , which you need.

So, the solution for your request will be like this if you call it from the controller object:

 $this->get('request')->query->remove('email_suffix'); 
+9
source

Source: https://habr.com/ru/post/922112/


All Articles