Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correction de régression sur dsfr_input_class_attr #190

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions dsfr/test/test_forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from typing import NoReturn

from django import forms
from django.test import SimpleTestCase


from dsfr.forms import DsfrBaseForm


class DsfrBaseFormTestCase(SimpleTestCase):
class TestForm(DsfrBaseForm):
book_format = forms.ChoiceField(
label="Format",
choices=(
("PAPER", "Papier"),
("NUM", "Numérique"),
),
)
book_format2 = forms.ChoiceField(
label="Format",
choices=(
("PAPER", "Papier"),
("NUM", "Numérique"),
),
widget=forms.RadioSelect,
)
user_id = forms.CharField(widget=forms.HiddenInput)

def test_init_sets_attributes(self):
form = self.TestForm(data={})
self.assertEqual(form.fields["book_format"].widget.attrs["class"], "fr-select")
self.assertEqual(
form.fields["book_format"].widget.group_class, "fr-select-group"
)
self.assertEqual(form.fields["book_format2"].widget.attrs["dsfr"], "dsfr")
self.assertEqual(
form.fields["book_format2"].widget.group_class, "fr-radio-group"
)
self.assertEqual(form.fields["user_id"].widget.attrs, {})

def test_init_dont_trigger_form_validation(self):
test_ctx = self

class AssertForm(self.TestForm):
@property
def errors(self) -> NoReturn:
test_ctx.fail("No validation expected")

# __init__ should not trigger validation
AssertForm(data={})

with self.assertRaises(self.failureException):
# is_valid should trigger validation
AssertForm(data={}).is_valid()
3 changes: 2 additions & 1 deletion dsfr/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ def dsfr_input_class_attr(bf: BoundField):
):
bf.field.widget.attrs["class"] = "fr-input"

if bf.errors:
# bf.errors triggers form validation. We need to check form._errors to prevent that
if bf.form._errors and bf.errors:
bf.field.widget.attrs.update(
{"aria-invalid": "true", "aria-describedby": f"{bf.auto_id}-desc-error"}
)
Expand Down
Loading