I\'ve got a form built in Symfony and when rendered in the view, the html form may or may not contain all of the fields in the form object (the entity sort of has a couple o
In short, don't use handleRequest
.
You should use submit
directly instead along with the clearMissing
parameter set to false.
Symfony/Component/Form/FormInterface
/**
* Submits data to the form, transforms and validates it.
*
* @param null|string|array $submittedData The submitted data.
* @param bool $clearMissing Whether to set fields to NULL
* when they are missing in the
* submitted data.
*
* @return FormInterface The form instance
*
* @throws Exception\AlreadySubmittedException If the form has already been submitted.
*/
public function submit($submittedData, $clearMissing = true);
When you use handleRequest
it works out what data you are wanting the submit and then submits it using $form->submit($data, 'PATCH' !== $method);
, meaning that unless you have submitted the form using the PATCH
method then it will clear the fields.
To submit the form yourself without clearing your can use...
$form->submit($request->get($form->getName()), false);
.. which get the form data array from the request and submit it directly, but with the clear missing fields parameter set to false.