Symfony3: How to update boolean to false using PATCH method

后端 未结 3 816
一个人的身影
一个人的身影 2021-01-18 09:20

I have a big entity and a big form. When updating my entity, I only render parts of my form, through ajax calls. On client side, I\'m using Jquery and html5 FormData

3条回答
  •  离开以前
    2021-01-18 09:45

    You are absolutely right, Symfony will ignore it if method is PATCH because of this line in Request Handler:

    $form->submit($data, 'PATCH' !== $method);
    

    Now, I would generally suggest that you use a PUT request if that is an option, but if it isn't then second argument to FormInterface::submit($submittedData, $clearMissing = true) is what you're after.

    The "proper" way would probably be to make your own implementation of Symfony\Component\Form\RequestHandlerInterface which would force $clearMissing to be true.

    Other, way is a lot easier but might not work for all use-cases: use $form->submit() directly.

    If you have the following code:

    $form->handleRequest($request);
    

    You can do:

    $form->submit($request->get($form->getName()), true);
    

    You can also omit second parameter since true is the default value

提交回复
热议问题