4

I'm building a REST API with FOSRestBundle in Symfony2. I'm using forms to create and update entities with Doctrine2. Everything works fine if I send all form fields. Example:

{"first_name":"Pi","last_name":"Wi"}

The person is inserted fine but now I want to update only the last name.

{"last_name":"Wi"}

The problem is that the first name is empty after the update because the form updates the entity with an "null" value (because it isn't given). Is it possible to just update the last name and ignore the first name?

Pi Wi
  • 1,076
  • 1
  • 11
  • 20

1 Answers1

10

Sure, it's possible.

First, in terms of RESTful that would be a PATCH request, so if you're using the ClassResourceInterface based controller approach, you'll have to add a patchAction method in your controller.

Then, when processing a submitted form, you'll need to pass a false $clearMissing option to your form's submit method call in the controller, like this:

<?php
// in your controller's patchAction:

/** @var \Symfony\Component\Form\FormInterface $form  */
/** @var \Symfony\Component\HttpFoundation\Request $request */

$form->submit($request, false);

This will tell the form component to only update the fields passed from the form, without clearing the missing fields (as the parameter's name says). See the source code for reference.

Note though, that passing a Request to a FormInterface::submit() method will be deprecated as of Symfony 3.0, so this answer is for Symfony 2.x.

kix
  • 3,290
  • 27
  • 39
  • from the docs: "the `submit()` method is deprecated and will be removed in Symfony 3.0" – NDM Sep 03 '14 at 07:56
  • @NDM, can you give an exact link? The only thing I've found on `submit()` is that >> Passing the Request directly to submit() still works, but is deprecated and will be removed in Symfony 3.0. See http://symfony.com/doc/current/cookbook/form/direct_submit.html#passing-a-request-to-form-submit-deprecated – kix Sep 03 '14 at 08:07
  • 1
    Thanks! But, it is deprecated ;-) However, you gave me the right keywords to do a new Google search and I found this at StackOverflow: http://stackoverflow.com/questions/19793767/does-symfony-2-support-partial-form-binding. You can use handleRequest and make sure the request method is `PATCH`. Because most browsers don't send a PATCH request you can override the method by using `_method`. Then it works out of the box! :-) I accept your answer because it's the best of the two given. – Pi Wi Sep 03 '14 at 08:10
  • When your form isn't valid (it isn't handling the request) that is because the Symfony form isn't handling PATCH methods (only GET and POST). A workaround is to set the `method` option to `PATCH`. Read this for more information: http://www.sroze.io/2013/10/30/symfony2-form-patch-requests-and-handleRequest-form-never-valid/ – Pi Wi Sep 03 '14 at 08:35
  • Deprecated in this case, is only relevant if you plan to move onwards to Symfony 3 with your current project. I wouldnt do it for existing projects, merely for new ones... – Steini Jan 16 '15 at 07:36
  • hello, guys, it's the year 2017, and the link to the Symfony version is 3.3. I didn't see any `deprecated` mark. Maybe they didn't remove it? – Yarco Jun 30 '17 at 06:29