Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I remove an individual parameter from a Symfony2 request object

Tags:

symfony

I have the following request object and would like to remove 'email_suffix' from a controller before binding to a form. Is this 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 have tried $request->request->remove("registration[email_suffix]");

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

For now, I am doing this:

$requestReg = $request->request->get('registration');
$requestReg['email'] = $requestReg['email'].'@'.$requestReg['email_suffix'];
unset($requestReg['email_suffix']);
$request->request->set('registration',$requestReg);
like image 934
codecowboy Avatar asked Aug 04 '12 10:08

codecowboy


1 Answers

There's the possibility to add and to remove the parameters from the request object in symfony2. You have to look at ParameterBag Component, there's such the method called remove($key), that's what you need.

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

$this->get('request')->query->remove('email_suffix');
like image 196
Farside Avatar answered Sep 28 '22 06:09

Farside