Skip to content

Commit

Permalink
Allow overriding form in {% dsfr_form %} (#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
christophehenry authored Jun 19, 2024
1 parent 8012c62 commit 273de73
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
16 changes: 16 additions & 0 deletions doc/forms.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,19 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields["password"].widget.attrs["class"] += " my custom class"
```

## Utilisation avec les `Formsets`

La balise `{% dsfr_form %}` prend un paramètre optionnel `form` qui permet de surcharger le formulaire à rendre, ce qui
est pratique lors de l'utilisation de `Formsets` :

```{ .django }
<form method="post">
{{ formset.management_form }}
<table>
{% for subform in formset %}
{{ dsfr_form subform }}
{% endfor %}
</table>
</form>
```
12 changes: 9 additions & 3 deletions dsfr/templatetags/dsfr_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -1488,17 +1488,23 @@ def _render_alert_tag(message):


@register.inclusion_tag("dsfr/form_snippet.html", takes_context=True)
def dsfr_form(context) -> dict:
def dsfr_form(context: Context, form=None) -> dict:
"""
Returns the HTML for a form snippet
```python
data_dict = {
"form": an optionnal form to render instead of the form already present in context
}
```
**Tag name**:
dsfr_form
**Usage**:
`{% dsfr_form %}`
"""
return context
""" # noqa
return context.update({"form": form}) if form else context


@register.inclusion_tag("dsfr/form_field_snippets/field_snippet.html")
Expand Down
36 changes: 36 additions & 0 deletions dsfr/test/test_templatetags.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django import forms
from django.test import SimpleTestCase
from django.template import Context, Template
from unittest.mock import MagicMock
Expand All @@ -11,6 +12,7 @@
INTEGRITY_JS_MODULE,
INTEGRITY_JS_NOMODULE,
)
from dsfr.forms import DsfrBaseForm
from dsfr.templatetags.dsfr_tags import concatenate, hyphenate


Expand Down Expand Up @@ -482,6 +484,40 @@ def test_consent_tag_rendered(self):
)


class DsfrFormTagTest(SimpleTestCase):
class TestForm(DsfrBaseForm):
test = forms.CharField(label="Ceci est un test")

class TestForm2(DsfrBaseForm):
test = forms.CharField(label="Ceci est un autre test")

context = Context({"form": TestForm(), "form2": TestForm2()})

def test_dsfr_form_renders(self):
rendered_template = Template(
"{% load dsfr_tags %} {% dsfr_form %}"
).render(self.context)
self.assertInHTML(
"""
<label for="id_test" class="fr-label">Ceci est un test*</label>
<input type="text" name="test" class="fr-input" required id="id_test">
""",
rendered_template,
)

def test_dsfr_form_renders_with_form_override(self):
rendered_template = Template(
"{% load dsfr_tags %} {% dsfr_form form2 %}"
).render(self.context)
self.assertInHTML(
"""
<label for="id_test" class="fr-label">Ceci est un autre test*</label>
<input type="text" name="test" class="fr-input" required id="id_test">
""",
rendered_template,
)


class DsfrContentTagTest(SimpleTestCase):
test_data = {
"alt_text": "Silhouette stylisée représentant le soleil au-dessus de deux montagnes.",
Expand Down

0 comments on commit 273de73

Please sign in to comment.