Skip to content

Commit

Permalink
refactor: use variable set on the form itself to enable custom validity
Browse files Browse the repository at this point in the history
instead of specifying it in the template
  • Loading branch information
angela-tran committed Apr 25, 2024
1 parent 1721a44 commit ccff754
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 3 deletions.
2 changes: 1 addition & 1 deletion benefits/core/templates/core/includes/form.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
$(this).trigger("submitting");
});

{% if use_custom_validity %}
{% if form.use_custom_validity %}
const validate = function(input_element) {
input_element.setCustomValidity(""); // clearing message sets input_element.validity.customError back to false

Expand Down
3 changes: 3 additions & 0 deletions benefits/eligibility/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def __init__(self, agency: models.TransitAgency, *args, **kwargs):
verifier_field.choices = [(v.id, None) for v in verifiers]
verifier_field.widget.selection_label_templates = {v.id: v.selection_label_template for v in verifiers}
verifier_field.widget.attrs.update({"data-custom-validity": _("Please choose a transit benefit.")})
self.use_custom_validity = True

def clean(self):
if not recaptcha.verify(self.data):
Expand Down Expand Up @@ -118,6 +119,7 @@ def __init__(
sub_widget.attrs.update({"maxlength": sub_max_length})
if sub_custom_validity:
sub_widget.attrs.update({"data-custom-validity": sub_custom_validity})
self.use_custom_validity = True

self.fields["sub"] = forms.CharField(
label=sub_label,
Expand All @@ -130,6 +132,7 @@ def __init__(
name_widget.attrs.update({"maxlength": name_max_length})
if name_custom_validity:
name_widget.attrs.update({"data-custom-validity": name_custom_validity})
self.use_custom_validity = True

self.fields["name"] = forms.CharField(label=name_label, widget=name_widget, help_text=name_help_text)

Expand Down
2 changes: 1 addition & 1 deletion benefits/eligibility/templates/eligibility/confirm.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ <h1>{{ form.headline }}</h1>

{% block inner-content %}
<div class="container">
<div class="row">{% include "core/includes/form.html" with form=form use_custom_validity=True %}</div>
<div class="row">{% include "core/includes/form.html" with form=form %}</div>
</div>
{% endblock inner-content %}
2 changes: 1 addition & 1 deletion benefits/eligibility/templates/eligibility/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ <h1>{% translate "Choose the transit benefit you would like to enroll in" %}</h1
{% endblock explanatory-text %}

{% block inner-content %}
{% include "core/includes/form.html" with form=form use_custom_validity=True %}
{% include "core/includes/form.html" with form=form %}
{% endblock inner-content %}
4 changes: 4 additions & 0 deletions tests/pytest/eligibility/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ def test_MSTCourtesyCard():
assert name_attrs["maxlength"] == 255
assert name_attrs["data-custom-validity"] == "Please enter your last name."

assert form.use_custom_validity


def test_SBMTDMobilityPass():
form = SBMTDMobilityPass(data={"sub": "1234", "name": "Barbara"})
Expand All @@ -31,3 +33,5 @@ def test_SBMTDMobilityPass():
name_attrs = form.fields["name"].widget.attrs
assert name_attrs["maxlength"] == 255
assert name_attrs["data-custom-validity"] == "Please enter your last name."

assert form.use_custom_validity

0 comments on commit ccff754

Please sign in to comment.