S1arl
S1arl

Reputation: 55

Onchange in Symfony2 form

This is my input type

->add(‘year’,  ChoiceType::CLASS, array(‘choices’ => $array, ‘attr’ =>  array(‘onchange’ => ‘this.form.submit()’)));

Onchange page is reloading and data is submitted. Then in controller I can access value like this:

$_POST[‘year’]. 

The thing is I would like to get $_POST in symfony’s way:

$form[‘year’]->getData();

I don’t know why only $_POST[‘year’] works and no result with $form[‘year’]->getData().

Upvotes: 1

Views: 1938

Answers (2)

Imanali Mamadiev
Imanali Mamadiev

Reputation: 2654

You can use for POST request :

$request->request->get('year');

For GET request:

$request->query->get('year');

For FILE queries:

$request->files.

Upvotes: 3

Rooneyl
Rooneyl

Reputation: 7902

You can get a single item from the form data like;

$year = $form->get('year')->getData();

In this example 'year' is the name given to the field you are asking for (as per your form builder)

Upvotes: 2

Related Questions