Skip to content

Fix dependent fields with checkbox #327

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

Open
wants to merge 1 commit 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
7 changes: 6 additions & 1 deletion django_select2/static/django_select2/django_select2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
})
}

Expand Down
43 changes: 43 additions & 0 deletions tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 3 additions & 1 deletion tests/testapp/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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},
),
Expand All @@ -226,6 +226,8 @@ class AddressChainedSelect2WidgetForm(forms.Form):
),
)

active = forms.BooleanField(required=False, initial=True)


class GroupieForm(forms.ModelForm):
class Meta:
Expand Down
1 change: 1 addition & 0 deletions tests/testapp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",)
Expand Down
Loading