Cover of the book Symfony 5: The Fast Track

Symfony 5: The Fast Track is the best book to learn modern Symfony development, from zero to production. +300 pages showcasing Symfony with Docker, APIs, queues & async tasks, Webpack, SPAs, etc.

How to Use the submit() Function to Handle Form Submissions

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:

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
// ...

public function new(Request $request): Response
    $task = new Task();
    $form = $this->createForm(TaskType::class, $task);

    if ($request->isMethod('POST')) {

        if ($form->isSubmitted() && $form->isValid()) {
            // perform some action...

            return $this->redirectToRoute('task_success');

    return $this->render('task/new.html.twig', [
        'form' => $form->createView(),


Forms consisting of nested fields expect an array in submit(). You can also submit individual fields by calling submit() directly on the field:



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.


When the second parameter $clearMissing is false, like with the “PATCH” method, the validation will only apply to the submitted fields. If you need to validate all the underlying data, add the required fields manually so that they are validated:

// 'email' and 'username' are added manually to force their validation
$form->submit(array_merge(['email' => null, 'username' => null], $request->request->all()), false);

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.