You have a controller receiving GET parameters and you care about how to process this data? That's a good reaction!
You may use directly a validator to validate the data but you may like to know it's super-easy to make a form to validate your data!
In my case, I will have code
and state
in the query.
class CallbackType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('state', TextType::class, ['constraints' => [new NotBlank()]])
->add('code', TextType::class, ['constraints' => [new NotBlank()]])
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefault('method', 'GET');
$resolver->setDefault('csrf_protection', false);
}
}
Notice that:
It's dead simple but has something special: you need to set ''
as a name for your form.
#[Route(path: '/callback', name: 'callback')]
public function callback(Request $request): Response
{
$form = $this->container->get('form.factory')->createNamed('', CallbackType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// Do your stuff
}
// ...
}
We are using a "not named" form to avoid having query params named like formName[code]
. It's often more convenient for GET queries (and in my case it's even mandatory!).
That's all!
Hope you liked it!