monkeyUser
monkeyUser

Reputation: 4671

Form isSubmitted False in symfony Form with Put Method

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.

Upvotes: 2

Views: 5905

Answers (1)

E.K.
E.K.

Reputation: 1055

The issue because of by default, handleRequest() method checks if form method (that has been configured for the form) is equal to request method. By default, it's POST for the form.

Just use setMethod function. $builder->setMethod('PUT') or you also can set it in default options in the form type class.

Upvotes: 5

Related Questions