From ef0ac884b9de7bbce6bdb9f09fe8c882d44af5e7 Mon Sep 17 00:00:00 2001 From: Cleiton Lima Date: Wed, 28 May 2025 15:49:13 -0300 Subject: [PATCH] Fix dependent_fields with checkbox --- .../static/django_select2/django_select2.js | 7 ++- tests/test_forms.py | 43 +++++++++++++++++++ tests/testapp/forms.py | 4 +- tests/testapp/models.py | 1 + 4 files changed, 53 insertions(+), 2 deletions(-) diff --git a/django_select2/static/django_select2/django_select2.js b/django_select2/static/django_select2/django_select2.js index f299e367..54018ab4 100644 --- a/django_select2/static/django_select2/django_select2.js +++ b/django_select2/static/django_select2/django_select2.js @@ -35,7 +35,12 @@ $.each(dependentFields, function (i, dependentField) { const nameIs = `[name=${dependentField}]` const nameEndsWith = `[name$=-${dependentField}]` - result[dependentField] = (findElement(nameIs) || findElement(nameEndsWith)).val() + const field = (findElement(nameIs) || findElement(nameEndsWith)) + if (field.is(":checkbox")) { + result[dependentField] = field.prop("checked") ? "1" : "0" + } else { + result[dependentField] = field.val() + } }) } diff --git a/tests/test_forms.py b/tests/test_forms.py index a2b87522..c3354471 100644 --- a/tests/test_forms.py +++ b/tests/test_forms.py @@ -844,6 +844,49 @@ def test_dependent_fields_clear_after_change_parent( ) assert city2_container.text == "" + @pytest.mark.selenium + def test_dependent_fields_using_checkbox( + self, db, live_server, driver, cities + ): + driver.get(live_server + self.url) + ( + country_container, + city_container, + city2_container, + ) = driver.find_elements(By.CSS_SELECTOR, ".select2-selection--single") + + # selecting a country really does it + city_container.click() + WebDriverWait(driver, 60).until( + expected_conditions.presence_of_element_located( + (By.CSS_SELECTOR, ".select2-results li") + ) + ) + city_option = driver.find_element( + By.CSS_SELECTOR, ".select2-results li" + ) + city_name = city_option.text + city_option.click() + assert city_name == city_container.text + + # check active to false + active_checkbox = driver.find_element( + By.ID, 'id_active' + ) + active_checkbox.click() + + # check the value in city + city_container.click() + WebDriverWait(driver, 60).until( + expected_conditions.presence_of_element_located( + (By.CSS_SELECTOR, ".select2-results li") + ) + ) + city_option = driver.find_element( + By.CSS_SELECTOR, ".select2-results li" + ) + assert city_option.text == "No results found" + @pytest.fixture( name="widget", diff --git a/tests/testapp/forms.py b/tests/testapp/forms.py index 0b115339..33200928 100644 --- a/tests/testapp/forms.py +++ b/tests/testapp/forms.py @@ -209,7 +209,7 @@ class AddressChainedSelect2WidgetForm(forms.Form): label="City", widget=ModelSelect2Widget( search_fields=["name__icontains"], - dependent_fields={"country": "country"}, + dependent_fields={"country": "country", "active": "active"}, max_results=500, attrs={"data-minimum-input-length": 0}, ), @@ -226,6 +226,8 @@ class AddressChainedSelect2WidgetForm(forms.Form): ), ) + active = forms.BooleanField(required=False, initial=True) + class GroupieForm(forms.ModelForm): class Meta: diff --git a/tests/testapp/models.py b/tests/testapp/models.py index 8138a9bc..55ea731a 100644 --- a/tests/testapp/models.py +++ b/tests/testapp/models.py @@ -59,6 +59,7 @@ class City(models.Model): country = models.ForeignKey( "Country", related_name="cities", on_delete=models.CASCADE ) + active = models.BooleanField(default=True, blank=True) class Meta: ordering = ("name",)