lewisjenkins / craft-dynamic-fields

Populate Craft fields with dynamic data using the power of Twig.
Other
147 stars 11 forks source link

Bug when displaying selected checkboxes #35

Closed bcartier closed 4 years ago

bcartier commented 4 years ago

I have a dynamic checkboxes field that allows selection of user group ids. This is the twig code that I'm using in the Checkbox Options in the field configuration:

{% set groups = craft.app.userGroups.allGroups %} {% for group in groups %} {"value":"{{ group.id }}" , "label":"{{ group.name | raw }}" }{% if not loop.last %},{% endif %}
{% endfor %}

It works great, stores the values correctly, etc. But there is a small bug when the values of the selected checkboxes are displayed in the form. If a checkbox with a value of "4" is saved in the database, it correctly marks "4" as checked as the form loads. The trouble is, it also marks "14" and "24" as checked, because "4" is found withing their value.

This becomes a problem when I make changes to other fields in the edit screen - the values "14" and "24" are being added to the field inadvertently because the form load with them checked in the UI. It can easily happen without noticing since I may not even be looking at that field. I had a client wondering where these extra values were coming from, and why he couldn't un-check item "14".

I'm using: Craft 3.3.9 LJ Dynamic Fields 3.0.10 The field in question is being used as a User custom field, if that makes any difference.

Thanks so much for your help!

bcartier commented 4 years ago

I should also mention that it happens the other direction too. If "14" is selected in the DB, the form loads showing 14, 1, and 4 as checked.

lewisjenkins commented 4 years ago

I've confirmed this and should have it fixed shortly...

lewisjenkins commented 4 years ago

I've just released a fix for this. Please update via Craft and all should be good...

bcartier commented 4 years ago

Thanks for the quick response!