From 1f9ea4a72f7a8de50bc9cedb30c221af98df6831 Mon Sep 17 00:00:00 2001 From: Katie Smith Date: Fri, 19 Feb 2021 11:31:25 +0000 Subject: [PATCH] Stop adding required attribute to WTForm fields MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit WTForms now renders the `required` attribute if there is a validator such as `DataRequired`. This was flagged in an accessibility audit as being unnecessary since it doesn't conform to the Design System recommendations, which state that "all form fields are considered mandatory when navigating a government service unless otherwise denoted by the word ‘(optional)’." This uses the approach here https://github.com/wtforms/wtforms/pull/361 to overwrite the `render_field` method. --- app/main/forms.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/main/forms.py b/app/main/forms.py index b49715f4ff..8f12595aab 100644 --- a/app/main/forms.py +++ b/app/main/forms.py @@ -555,6 +555,10 @@ def bind_field(self, form, unbound_field, options): bound.get_form = weakref.ref(form) # GC won't collect the form if we don't use a weakref return bound + def render_field(self, field, render_kw): + render_kw.setdefault('required', False) + return super().render_field(field, render_kw) + class StripWhitespaceStringField(GovukTextInputField): def __init__(self, label=None, param_extensions=None, **kwargs):