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

fixed bug which caused bound forms to lose selected option #89

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion crispy_tailwind/templates/tailwind/layout/select.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<div class="relative">
<select class="{% if field.errors %}border border-red-500 {% endif %}bg-white focus:outline-none border border-gray-300 rounded-lg py-2 px-4 block w-full appearance-none leading-normal text-gray-700" name="{{ field.html_name }}" {{ field.field.widget.attrs|flatatt }}>
{% for value, label in field.field.choices %}
{% include "tailwind/layout/select_option.html" with value=value label=label %}
{% include "tailwind/layout/select_option.html" with value=value|stringformat:'s' label=label %}
{% endfor %}
</select>
<div class="pointer-events-none absolute inset-y-0 right-0 flex items-center px-2 text-gray-700">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% load crispy_forms_filters %}
{% load l10n %}

<option value="{{ value|stringformat:'s' }}" {{ field.field.widget.attrs|flatatt }}{% if field.value == value %} selected{% endif %}>{{ label }}</option>
<option value="{{ value }}" {{ field.field.widget.attrs|flatatt }}{% if field.value|stringformat:'s' == value %} selected{% endif %}>{{ label }}</option>
8 changes: 8 additions & 0 deletions tests/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,11 @@ class SelectForm(forms.Form):
widget=forms.Select(),
choices=(("accepted", "Accepted"), ("not_accepted", "Not accepted")),
)


class AgeRangeSelectForm(forms.Form):
age_range = forms.ChoiceField(
label="age range",
widget=forms.Select(),
choices=((0, "18-24"), (1, "25-44"), (2, "45+")),
)
25 changes: 25 additions & 0 deletions tests/test_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from crispy_tailwind.layout import Button, Reset, Submit

from .forms import (
AgeRangeSelectForm,
CharFieldForm,
CheckboxMultiple,
PasswordFieldForm,
Expand Down Expand Up @@ -572,3 +573,27 @@ def test_select(self):
</div>
"""
self.assertHTMLEqual(html, expected_html)

def test_select_option_preserved_on_bound_forms(self):
form = AgeRangeSelectForm(data={"age_range": 1})
form.helper = FormHelper()
form.helper.form_tag = False
html = render_crispy_form(form)
expected_html = """
<div id="div_id_age_range" class="mb-3">
<label for="id_age_range" class="block text-gray-700 text-sm font-bold mb-2"> age range<span class="asteriskField">*</span> </label>
<div class="mb-3">
<div class="relative">
<select class="bg-white focus:outline-none border border-gray-300 rounded-lg py-2 px-4 block w-full appearance-none leading-normal text-gray-700" name="age_range">
<option value="0">18-24</option>
<option value="1" selected>25-44</option>
<option value="2">45+</option>
</select>
<div class="pointer-events-none absolute inset-y-0 right-0 flex items-center px-2 text-gray-700">
<svg class="fill-current h-4 w-4" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20"><path d="M9.293 12.95l.707.707L15.657 8l-1.414-1.414L10 10.828 5.757 6.586 4.343 8z" /></svg>
</div>
</div>
</div>
</div>
"""
self.assertHTMLEqual(html, expected_html)