Iter Ator
Iter Ator

Reputation: 9334

Why is form-getData() empty? (Symfony2)

There is a HTML form, whish contains an email and password field. I would like to send it to my Controller action using POST.

It only works, if I access the inputs using $request->request->get('email')

This code doesn't works. $data is an empty object for some reason:

$form = $this->createFormBuilder()
        ->add('email', 'email')
        ->add('pass', 'text')
        ->getForm();

$form->handleRequest($request);
$data = $form->getData();

return new Response('email: '.$data['email']);

Upvotes: 1

Views: 1328

Answers (2)

rck6982
rck6982

Reputation: 239

Try this:

$form->get('email')->getData();

in your twig template:

<form action="{{ path('path-to-controller') }}" method="POST">
{{ form_widget }}
<input type="submit" value="send"/>
</form>

Upvotes: 3

Emii Khaos
Emii Khaos

Reputation: 10085

This is because the form does not expect email as POST parameter.

If you are creating the form inputs manually you should render the form in twig first to know the correct form input field names.

Upvotes: 0

Related Questions