I use form.factory to create a form and twig to render the form. I have to use PUT method in this case. My code is:
Controller:
$builder = $this->get('form.factory')->createNamedBuilder();
$form = $builder
->add('id', HiddenType::class, array('data' => $id))
->add('email', EmailType::class, array(
'required' => false,
'data' => count($res['result'][0]['email']) ? $res['result'][0]['email'] : '',
'attr' => array('class' => 'form-control label_form_symfony'),
'constraints' => array(
new Email(array('message' => 'il campo Email non è valido'))
)
))
->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $request->isXmlHttpRequest()) {
$data = $form->getData();
if ($form->isValid()) {
// save ...
}
}
Twig template:
{{ form_start(form,{'method':'PUT','attr':{action: path('update_xxx',{'id' : id})}}) }}
{{ form_widget(form, {'attr' : {'class' : 'label_form_symfony' } } ) }}
<button type="submit" class="submit_form btn btn-default">Save</button>
<div id="feedback"></div>
{{ form_end(form) }}
When I submit the form my $request->getMethod()is PUT but my $form->isSubmitted() is false.
In my HTML I have even the hidden field:
<input type="hidden" name="_method" value="PUT">
Update
I added
$builder->setMethod('PUT') in my controller and remove 'method':'PUT'from my twig, when I submit the form I get this error This form should not contain extra fields.