The CSRF token is invalid. Please try to resubmit the form

The CSRF token is invalid. Please try to resubmit the form

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

The CSRF token is invalid. Please try to resubmit the form

When you encounter the error message The CSRF token is invalid. Please try to resubmit the form, it signals a mismatch between the security token sent by your browser and the one expected by your server. This mechanism is fundamental to preventing Cross-Site Request Forgery (CSRF) attacks, but in development environments or specific deployment scenarios, it often manifests as a friction point rather than a security vulnerability.

The root cause lies in how Symfony forms and Laravel's CSRF middleware interact. Your form code uses {{form_enctype(form)}}, which correctly sets the enctype="multipart/form-data" attribute. This is necessary if you intend to upload files, but it does not inherently solve token validation issues. The error occurs because the HTML form submitted lacks a valid _token input field, or the value of that field in the hidden input does not match the session key stored on the server.

In modern Symfony applications running within Laravel, the CSRF protection is usually handled automatically by the framework's request middleware. However, when rendering a form using Twig with Symfony Forms, you must explicitly include the token if your form does not automatically inherit it from the layout or controller context. The most common oversight happens when the global {{ csrf_token() }} macro is missing from the specific form block or the main layout template where the form is rendered.

To resolve this immediately, ensure that your base Twig template includes the CSRF token for every POST request. This looks like:

{% block body %}
    {{ form_start(form) }}
        {# Your form fields here #}
        {{ csrf_token() }}
    {{ form_end(form) }}
{% endblock %}

If you are manually constructing the form HTML using form_widget as shown in your snippet, you must add the hidden input explicitly. The token is typically generated by the form_start macro, which wraps the entire form content. If you bypass form_start and call form_label and form_widget individually, or if you are rendering the form outside of a form_start/form_end block without using the {% csrf_token %} tag, the server will reject the submission.

There is also a specific edge case involving the enctype attribute. While {{form_enctype(form)}} ensures file uploads work, some older versions of Symfony or misconfigured setups might strip this attribute if not handled correctly in the view. However, the token error is distinct from an upload error; it is purely about session integrity.

Another frequent culprit involves session configuration. If you are running your application behind a load balancer or using multiple servers, ensure that sessions are stored centrally (e.g., in Redis) or that cookies are configured to be shared across nodes. If each server has a different session store, the token generated on Server A might not match the expectation on Server B, causing a false positive "invalid token" error even if the form itself is correct.

Finally, check your environment variables. The APP_SECRET key in your .env file is critical for generating secure random tokens. If this value is weak or missing in development, Laravel might generate predictable tokens, which, while not breaking validation logic immediately, can lead to confusion during debugging. Ensure you have a strong secret configured:

APP_SECRET=your_secure_random_string_here

If the issue persists after verifying the token inclusion and session configuration, inspect the php.ini or Laravel config for session.use_strict_mode. Strict mode can sometimes interfere with cookie handling depending on your browser settings.

For further guidance on securing forms in Symfony projects hosted on Laravel, refer to the resources available at https://laravelcompany.com, which often covers best practices for integrating these frameworks securely.

Our network