Skip to content

Commit

Permalink
Stop adding required attribute to WTForm fields
Browse files Browse the repository at this point in the history
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 wtforms/wtforms#361
to overwrite the `render_field` method.
  • Loading branch information
klssmith committed Feb 24, 2021
1 parent a2147e9 commit 1f9ea4a
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions app/main/forms.py
Expand Up @@ -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):
Expand Down

0 comments on commit 1f9ea4a

Please sign in to comment.