When i submit a json http post request in symfony4 the requesthandling not submitting my form.
I tried setting the "method" option to the form to post, but it doesn't seem to help. I'm using the application/json header for Content-type and Accept.
The form:
class SecurityUserForm extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('username', TextType::class)
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => SecurityUser::class,
'csrf_protection' => false,
]
);
}
}
The controller:
public function postAction(Request $request, SerializerInterface $serializer, UserPasswordEncoderInterface $encoder)
{
try {
$user = new SecurityUser();
$form = $this->createForm(SecurityUserForm::class, $user,
array(
'method' => 'POST'
));
$form->handleRequest($request); //NOT SUBMITTING THE FORM
if ($form->isSubmitted() && $form->isValid()) {
dump("submitted");
} else {
//Stuff
}
} catch (\Exception $e) {
//Exception handling stuff
}
}
And my http post:
Headers: Content-Type:application/json Accept:application/json
Body: { "username": "dunglas" }
I expect the $form->handleRequest($request) to submit the form, but for some reason it doesn't. When i dump after handling the request
dump($form->isSubmitted()) //false
it returns false. What am i doing wrong?