0

Select2 dropdowns are broken after editing a record — how to fix?

When I edit an existing record, the Select2 field shows the wrong value or is blank. This seems to happen only with the full AJAX controller type. Is this a known bug?
Posee par Inconnu Feb 06, 2026 111 vues

1 reponse

0
✓ Acceptee
This is a known issue in full-AJAX controller mode (ResourceController with modal forms). The root cause is that Select2 needs to be re-initialized after the modal content is injected into the DOM — the initial page load initialization does not apply to dynamically rendered form fields. Workarounds: 1. In your modal 'shown' event handler, call `$('#your-select2-field').select2()` after the AJAX response populates the modal. 2. Alternatively, destroy and reinitialize: `$('select').select2('destroy').select2()`. 3. The safest fix for edit forms specifically is to ensure the AJAX response that loads the edit form includes the current value as the selected option in the HTML, not relying on Select2 to set it programmatically after initialization. For new projects, if you have a module with complex dropdowns and you need reliable edit behavior, use the non-AJAX controller type instead. It does not have this problem.
Repondu par cantonner Feb 06, 2026

Veuillez vous connecter pour publier une reponse. vous connecter