Skip to content

Commit

Permalink
Add SelectRequired ChoiceField widget with disabled entry
Browse files Browse the repository at this point in the history
Adds a ChoiceField widget that allows the default (empty) option to be
disabled, so that the user is encouraged to select one of the other
options. The option is added to the "Do you identify as Autistic" field.

Contributes to #569
  • Loading branch information
llewelld committed Aug 29, 2023
1 parent 58418f2 commit 5be71a8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
8 changes: 7 additions & 1 deletion server/apps/users/forms.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
from django import forms

class SelectRequired(forms.Select):
# Disables items in the drop down list that have an empty value
# See https://docs.djangoproject.com/en/4.2/ref/forms/renderers/#built-in-template-form-renderers
# See also the ChoiceWidget.create_option() implementation in django/forms/widgets.py
option_template_name = "users/select_option_disable_empty.html"

class UserProfileForm(forms.Form):

autistic_identifications = [
Expand All @@ -8,7 +14,7 @@ class UserProfileForm(forms.Form):
("no", "No"),
("unspecified", "Prefer not to say"),
]
autistic_identification = forms.ChoiceField(choices = autistic_identifications, widget = forms.Select(),
autistic_identification = forms.ChoiceField(choices = autistic_identifications, widget = SelectRequired(),
label="Do you identify as autistic?",
help_text="AutSPACEs is focusing on collecting and sharing the voices and lived experiences of autistic people, which is why this is a mandatory question. If you select <i>prefer not to say</i> your answers will be considered as coming from a non-autistic person, labeled as such and e.g. not used for research.",
required=True)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<option value="{{ widget.value|stringformat:'s' }}"{% include "django/forms/widgets/attrs.html" %}{% if widget.value|stringformat:'s' == '' %} disabled="disabled"{% endif %}>{{ widget.label }}</option>

0 comments on commit 5be71a8

Please sign in to comment.