Skip to content

Commit dd489df

Browse files
authored
fix: [LiveComponent] CSRF token not valid after form second submit
Fix the “invalid CSRF token” error when working with live-components and submitting a form a second time, for example if a server-side validation fails and the form is re-rendered.
1 parent b04a17d commit dd489df

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

symfony/stimulus-bundle/2.20/assets/controllers/csrf_protection_controller.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ export function generateCsrfToken (formElement) {
3333
if (!csrfCookie && nameCheck.test(csrfToken)) {
3434
csrfField.setAttribute('data-csrf-protection-cookie-value', csrfCookie = csrfToken);
3535
csrfField.defaultValue = csrfToken = btoa(String.fromCharCode.apply(null, (window.crypto || window.msCrypto).getRandomValues(new Uint8Array(18))));
36-
csrfField.dispatchEvent(new Event('change', { bubbles: true }));
3736
}
37+
csrfField.dispatchEvent(new Event('change', { bubbles: true }));
3838

3939
if (csrfCookie && tokenCheck.test(csrfToken)) {
4040
const cookie = csrfCookie + '_' + csrfToken + '=' + csrfCookie + '; path=/; samesite=strict';

0 commit comments

Comments
 (0)