get data into array using a form with symfony 3

I feel kind of stupid here because, I've done some complex stuff in symfony 3, but I don't know, there's something simple that I'm missing when trying to so just easy tasks.

This should be pretty simple but I can't make it to work.

Getting data from a request in get method is quite simple. Just using "request->query('param')" it's enough.

But I want to create an indexed array with these params using a formtype or a form and take advantage of getting data converted in to the correct type automatically.

I have this array:

$booking = ['dateIn' => null, 'dateOut' => null]

And this form

$form = $this->createFormBuilder($booking, ['csrf_protection' => false])
            ->setMethod('GET')
            ->add('dateIn', TextType::class)
            ->add('dateOut', TextType::class)
            ->getForm();

When I dump the data after calling

"/book?dateIn=xxxx&dateOut=xxxxx"

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

It's still

['dateIn' => null, 'dateOut' => null]

I used TextType instead of DateType to make it simple and discard other problems when testing this thing. But my goal is to get this params converted too.

What's wrong? Should I make it "simpler" (avoiding forms) or use a real booking class, etc. I don't want to use any security complexity as I don't need it here.

Thanks!

Upvotes: 1

Views: 903

Answers (1)

Matteo
Matteo

Reputation: 39370

The default form name is form so i this this should work with params like:

/book?[form]dateIn=xxxx&[form]dateOut=xxxxx

You can modify the form name with the formFactory services, as example:

$form = $this->get('form.factory')->createNamedBuilder('','Symfony\Component\Form\Extension\Core\Type\FormType', $booking, ['csrf_protection' => false])
            ->setMethod('GET')
            ->add('dateIn', TextType::class)
            ->add('dateOut', TextType::class)
            ->getForm();

Or create a classic form type and implement the getBlockPrefix method and return an empty string.

Hope this help

Upvotes: 1

Related Questions