Skip to content

Commit

Permalink
Merge pull request #577 from llewelld/iss569_disable_empty_option
Browse files Browse the repository at this point in the history
Add SelectRequired ChoiceField widget with disabled entry
  • Loading branch information
gedankenstuecke authored Aug 29, 2023
2 parents 58418f2 + 5be71a8 commit 41dbd2d
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 41dbd2d

Please sign in to comment.