symfony: Form handleRequest() does not work properly.

From symfony docs:

New in version 2.3: The handleRequest() method was added in Symfony 2.3. Previously, the $request was passed to the submit method - a strategy which is deprecated and will be removed in Symfony 3.0. For details on that method, see Passing a Request to Form::submit() (deprecated).

I’m confused with form binding. In docs says that submit method is deprecated, but when I generate CRUD with symfony generator, it puts bind method for form handling. In code above bind method placed comment that contains next:

     * Alias of {@link submit()}.
     * @deprecated Deprecated since version 2.3, to be removed in 3.0. Use {@link submit()} instead.

So, two functions was deprecated? Okay, when I use handleRequest in my controllers it does not work with forms submitted with PUT method. What method will correctly use for form handling?

About this issue

  • Original URL
  • State: closed
  • Created 11 years ago
  • Comments: 39 (23 by maintainers)

Commits related to this issue

Most upvoted comments

Confirming the same issue with PUT method

In my case isSubmitted() return false.

I noticed that in: Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationRequestHandler line 40 $method = $form->getConfig()->getMethod() is “POST” $request->getMethod() is “PUT”

so handleRequest returns null in line 41.

To solve temporarily there are many options:

  • create the form passing an options array in your CRUD controllers:

$myForm = $this->createForm(new MyType(), $myEntity, array(‘method’ => ‘PUT’));

  • using a standard POST request

basically the value for the hidden input “_method” must match the form method.

3 years later and $form->handleRequest($request) followed by form->isValid() still returns false with no error message at all if you use PUT without also having created the form with PUT method. Nowhere to be seen in the docs. Wasted 2 hours on this.

If you use the handleRequest method, the HTTP method indeed needs to match the method set in the form (either through the method option when building it or through setMethod after that), otherwise it will not be bound

the doc does not say that submit() is deprecated (it is not). It says that passing the Request object to submit() directly is deprecated (but will still work until 3.0)

well IMHO the biggest problem here is it fails silently. users must spend time to investigate what went wrong and that’s just a PITA. i understand that throwing an error would be a BC, but you should understand that silent fail is just wrong. also, this problem still exists.

to solve this, the handleRequest should behave like this:

  • if the form’s method IS NOT set in the form config (ie. is NULL), then handleRequest should be method agnostic, ie. accept ANY used method

  • if the form’s method IS set, and the used method does not match, it should log an error into logger rather than throwing an error (to avoid BC)

Compose email

On Jul 12, 2017 12:12 PM, “Abdessamad Idrissi” notifications@github.com wrote:

The reason for me why the form is not submited is because of this line:

// Don’t submit the form if it is not present in the request return;

at vendor/symfony/symfony/src/Symfony/Component/Form/ Extension/HttpFoundation/HttpFoundationRequestHandler.php:101

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/symfony/symfony/issues/8261#issuecomment-314819145, or mute the thread https://github.com/notifications/unsubscribe-auth/AOXMGAzoE0N3v7r0RHwemynXtOq5rdArks5sNPB6gaJpZM4Auscn .

If’ve the same Problem with PUT!

The doc says to use and it should be ok. So there is a bug with handleRequest This issue should not to be closed!

Hack: in Controller Action set the method explizit: $editForm = $this->createForm(new DepartementType(), $entity, array(‘method’ => ‘PUT’));

I’m having the same issue. The following PUT action controller does not save the new $object (and $form->isValid() incorrectly returns false):

    public function putAction(Object $object, Request $request) {
        $form = $this->createForm(new ObjectType(), $object);
        $form->handleRequest($request);

        if (!$form->isValid()) {
            throw new HttpException(Codes::HTTP_BAD_REQUEST);
        }

        $em = $this->getDoctrine()->getManager();
        $em->flush();
    }

But by replacing $form = $this->createForm(new ObjectType(), $object); with $form = $this->createForm(new ObjectType(), $object, array(‘method’ => ‘PUT’)); the method works as expected.