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 cead111 commit e0f296e
Show file tree
Hide file tree
Showing 4 changed files with 5 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
2 changes: 2 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 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 @@ -21,6 +21,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 %}

0 comments on commit e0f296e

Please sign in to comment.