0

I have a standard login form which I built with the createFormBuilder() method. When I try to fetch input with

$this->get('request')->request->get('userName'); 

I get NULL beacuse $this->get('request')->request is a ParameterBag object who only has 'form' field with the input values. So when i do...

$this->get('request')->request->get('form')

i get the desired results but in an array

array(
    'userName' => 'Dude',
    'password' => 'password'
);

which is fine but is there a more intuitive way of fetching POST data?

I found this StackOverflow answer but it didn't help.

I also tried with the Request object as a parameter in the controller method but with no results.

Community
  • 1
  • 1
Mario Legenda
  • 749
  • 1
  • 11
  • 24
  • How is that *"not intuitive"*? You now KNOW that `request` contains `POST` data. What are you trying to achieve? – Touki Feb 27 '14 at 15:23
  • I started sympfony2 3 day ago and I remember that I could get the data with $request->get('userName'). I know that there's not one way to do something in symfony so I thought that there's another way to do this. – Mario Legenda Feb 27 '14 at 15:27
  • 2
    If you're trying to get `userName` directly from the request object, you can do `$request->request->get('form[userName]', null, true)` or `$request->get('form[userName]', null, true)`. Calling `Request::get` will look through `$request->query`, then `$request->attributes`, then `$request->request` – Touki Feb 27 '14 at 15:30
  • I tried that function but i didn't see the $default parameter. Thanks for the help. – Mario Legenda Feb 27 '14 at 15:33
  • `$form->bind($request); $data = $form->getData(); echo $data['userName']`? – putvande Feb 27 '14 at 16:11

1 Answers1

0

Basically, I will firstly verify whether a form is valid or not. Then, I will bind the form object and use that form to get some data. So, the code should be looked like following:

if ($request->getMethod('post') == 'POST') {
        // Bind request to the form
        $form->submit($request);
        // If form is valid
        if ($form->isValid()) {
            // Get data from the userName field
            $username = $form->get('userName')->getData();            
        }
}
lvarayut
  • 13,963
  • 17
  • 63
  • 87