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

Add ability to specify the form for radio input #304

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}>
<label class="form-check-label" for="{{ option.attrs.id }}">{{ option.label }}</label>
</div>
Expand Down
31 changes: 31 additions & 0 deletions tests/test_bootstrap_field_radio_select.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -111,3 +120,25 @@ def test_disabled_select(self):
"</div>"
),
)

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()}),
(
'<div class="django_bootstrap5-req mb-3">'
'<label class="form-label">Test</label>'
'<div class="" disabled required id="id_test">'
'<div class="form-check">'
'<input class="form-check-input" form="another-form" type="radio" name="test" id="id_test_0" value="1">'
'<label class="form-check-label" for="id_test_0">one</label>'
"</div>"
'<div class="form-check">'
'<input class="form-check-input" form="another-form" type="radio" name="test" id="id_test_1" value="2">'
'<label class="form-check-label" for="id_test_1">two</label>'
"</div>"
"</div>"
"</div>"
),
)