From 030cee99e308c03a57cd26ca7330c8b83b6673b2 Mon Sep 17 00:00:00 2001 From: "Paul (xobb) Chubatyy" Date: Sat, 7 May 2022 22:02:47 -0700 Subject: [PATCH 1/2] Add ability to specify the form for radio input Form inputs may be associated with different forms. It allows creating "forms within forms". Read a definition on [HTML spec](https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fae-form). One can specify the form the input will be associated with like this: ```python sex = forms.ChoiceField( widget=forms.RadioSelect(attrs={"form": "order-create-form"}), choices=Order.SEX_CHOICES, label=_("Sex"), ) ``` --- .../templates/django_bootstrap5/widgets/radio_select.html | 1 + 1 file changed, 1 insertion(+) diff --git a/src/django_bootstrap5/templates/django_bootstrap5/widgets/radio_select.html b/src/django_bootstrap5/templates/django_bootstrap5/widgets/radio_select.html index a6441a56..0bf65c86 100644 --- a/src/django_bootstrap5/templates/django_bootstrap5/widgets/radio_select.html +++ b/src/django_bootstrap5/templates/django_bootstrap5/widgets/radio_select.html @@ -12,6 +12,7 @@ id="{{ option.attrs.id }}" {% if option.value != None %} value="{{ option.value|stringformat:'s' }}" {% if option.attrs.checked %} checked="checked"{% endif %}{% endif %} + {% if widget.attrs.form %} form="{{ widget.attrs.form }}{% endif %} {% if widget.attrs.disabled %} disabled{% endif %}> From 1b00d5f99face282c3d1adb65125541f596881ab Mon Sep 17 00:00:00 2001 From: "Paul (xobb) Chubatyy" Date: Sat, 7 May 2022 22:08:54 -0700 Subject: [PATCH 2/2] Add test for the use case --- tests/test_bootstrap_field_radio_select.py | 31 ++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tests/test_bootstrap_field_radio_select.py b/tests/test_bootstrap_field_radio_select.py index dc1b0a86..b459414b 100644 --- a/tests/test_bootstrap_field_radio_select.py +++ b/tests/test_bootstrap_field_radio_select.py @@ -22,6 +22,15 @@ class DisabledSelectTestForm(forms.Form): widget=forms.RadioSelect, disabled=True, ) + + +class SelectOtherTestForm(forms.Form): + test = forms.ChoiceField( + choices=( + (1, "one"), + (2, "two"), + ), + widget=forms.RadioSelect(attrs={"form": "another-form"}) class BootstrapFieldSelectTestCase(BootstrapTestCase): @@ -111,3 +120,25 @@ def test_disabled_select(self): "" ), ) + + def test_other_form_select(self): + """Test field with select that belongs to another form widget.""" + self.maxDiff = None + self.assertHTMLEqual( + self.render("{% bootstrap_field form.test %}", context={"form": DisabledSelectTestForm()}), + ( + '
' + '' + '
' + '
' + '' + '' + "
" + '
' + '' + '' + "
" + "
" + "
" + ), + )