1

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!

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks!! the form name!!! since I always use post or json encapsulated data to send data to controllers I didn't notice that simple but important detail.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.