From 5caa0be8623029e9f767ddd2b19813410df56f49 Mon Sep 17 00:00:00 2001 From: Alice Gaudon Date: Mon, 25 Jan 2021 17:26:14 +0100 Subject: [PATCH] FormHelperComponent: add field ID prefix to prevent conflicts Fixes #20 --- src/components/FormHelperComponent.ts | 9 +++++++++ views/auth/auth.njk | 2 ++ views/macros.njk | 13 +++++++------ 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/components/FormHelperComponent.ts b/src/components/FormHelperComponent.ts index 93f0693..3f46ace 100644 --- a/src/components/FormHelperComponent.ts +++ b/src/components/FormHelperComponent.ts @@ -23,6 +23,15 @@ export default class FormHelperComponent extends ApplicationComponent { return _previousFormData; }; + + let _formPrefix: string | null; + res.locals.getFormPrefix = () => { + return _formPrefix; + }; + res.locals.setFormPrefix = (formPrefix: string) => { + _formPrefix = formPrefix; + return ''; + }; next(); }); diff --git a/views/auth/auth.njk b/views/auth/auth.njk index eac53d9..9accf33 100644 --- a/views/auth/auth.njk +++ b/views/auth/auth.njk @@ -18,6 +18,7 @@

Log in

+ {{ setFormPrefix('login-') }}
{{ macros.field(_locals, 'text', 'identifier', query.identifier or '', 'Your email address or username', null, 'required') }} @@ -34,6 +35,7 @@

Register

+ {{ setFormPrefix('register-') }} {{ macros.csrf(getCsrfToken) }} diff --git a/views/macros.njk b/views/macros.njk index b62170b..8396aea 100644 --- a/views/macros.njk +++ b/views/macros.njk @@ -37,6 +37,7 @@ {% set validation = validation[name] if validation[name] or null %} {% set previousFormData = _locals.previousFormData() %} {% set value = previousFormData[name] or value or validation.value or '' %} + {% set prefix = _locals.getFormPrefix() | default('no-') %} {% if type == 'hidden' %} {% if validation %} @@ -63,16 +64,16 @@ {% else %} {% set v = (value % 60) if f == 's' else (((value - value % 60) / 60 % 60) if f == 'm' else ((value - value % 3600) / 3600 if f == 'h')) %} {% endif %} - - + {% endfor %} {% elseif type == 'select' %} - {% for option in extraData %} @@ -80,16 +81,16 @@ {% elseif type == 'textarea' %} - {% else %} - {% endif %} - + {{ fieldError(_locals, name) }}