2

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.

0

1 Answer 1

5

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.

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

5 Comments

with $builder->setMethod('PUT') I have this error This form should not contain extra fields.
because you forgot remove <input type="hidden" name="_method" value="PUT"> -- that's an extra field.
I don't add <input type="hidden" name="_method" value="PUT"> Symfony does it for me. Now I remove 'method':'PUT'form Twig and add $builder->setMethod('PUT') and In my Html I have <input type="hidden" name="_method" value="PUT"> because is Symfony that does it
That kind of error (this form should not contain extra fields) means that on validation you get more fields than were declared. At least you can set allow_extra_fields to true at form options (i.e. when you create form builder)
I'd used its own Form Type (for example, AppBundle\UpdateXXXType) and then in controller run something like $form = $this->createForm(UpdateXXXType::class, new UpdateXXXModel());. In generally, you should avoid building forms inside the controllers, because if it breaks S.O.L.I.D. principles (controllers must be responsible for request and response processing only)

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.