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
- Add note about changed form processing when using PUT requests See https://github.com/symfony/symfony/issues/8261 — committed to fnagel/symfony by fnagel 6 years ago
- Add note about changed form processing when using PUT requests See https://github.com/symfony/symfony/issues/8261 — committed to fnagel/symfony by fnagel 6 years ago
- Add note about changed form processing when using PUT requests See https://github.com/symfony/symfony/issues/8261 — committed to fnagel/symfony by fnagel 6 years ago
- Add note about changed form processing when using PUT requests See https://github.com/symfony/symfony/issues/8261 — committed to fnagel/symfony by fnagel 6 years ago
- minor #27560 Add note about changed form processing when using PUT requests (fnagel) This PR was merged into the 2.8 branch. Discussion ---------- Add note about changed form processing when using ... — committed to symfony/symfony by fabpot 6 years ago
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:
$myForm = $this->createForm(new MyType(), $myEntity, array(‘method’ => ‘PUT’));
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 themethod
option when building it or throughsetMethod
after that), otherwise it will not be boundthe doc does not say that
submit()
is deprecated (it is not). It says that passing the Request object tosubmit()
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:
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):
But by replacing $form = $this->createForm(new ObjectType(), $object); with $form = $this->createForm(new ObjectType(), $object, array(‘method’ => ‘PUT’)); the method works as expected.