How to Use the submit() Function to Handle Form SubmissionsΒΆ
The recommended way of processing Symfony forms is to
use the handleRequest()
method
to detect when the form has been submitted. However, you can also use the
submit()
method to have better
control over when exactly your form is submitted and what data is passed to it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | use Symfony\Component\HttpFoundation\Request;
// ...
public function new(Request $request)
{
$form = $this->createForm(TaskType::class, $task);
if ($request->isMethod('POST')) {
$form->submit($request->request->get($form->getName()));
if ($form->isSubmitted() && $form->isValid()) {
// perform some action...
return $this->redirectToRoute('task_success');
}
}
return $this->render('task/new.html.twig', [
'form' => $form->createView(),
]);
}
|
Tip
When submitting a form via a "PATCH" request, you may want to update only a few
submitted fields. To achieve this, you may pass an optional second boolean
argument to submit()
. Passing false
will remove any missing fields
within the form object. Otherwise, the missing fields will be set to null
.
Caution
When the second parameter $clearMissing
is false
, like with the
"PATCH" method, the validation extension will only handle the submitted
fields. If the underlying data needs to be validated, this should be done
manually, i.e. using the validator.