theatlantic/django-select2-forms

Events are binded to the wrong form when adding an inline form in javascript

cirotix opened this issue · 3 comments

(django: 1.8.9, django-select2-forms: HEAD)

How to reproduce:

  • Create an admin view with a M2M in an inline form (should do with both tabular and stacked, but tested only with stacked)
  • The inline form has a select2 field (should do with all the select2 fields but tested only with Select2ManyToMany)
  • Open the admin view
  • In the inline add a new form with the "Add anothert" link
  • A new form is added in javascript
  • Click in the select2 field to open the widget

Observed behavior

  • The widget open in the top left of the page
  • You can select the options of the widget
  • The options do not fill in the input of the new form
  • When you save form the selected options are not saved

Expected behavior

  • The widget should work for the dynamically added forms as for a regular form

Explanation

The event is binded to the hidden form that is used to instanciate the new form when the user click on the "Add another". When a new form is instanciated its id is dynamically changed.
When the event is trigger it still target the hidden form and not the new one. You can see that the options are added to the hidden form rather than the new one.

How to solve

Make sure that the events are not binded to the hidden form
Dynamically bind the field on the dynamic form. This should be easier with the new`formset:addedandformset:removed`` jQuery events in Django 1.9 django/django@1335aa2

Btw, this is pretty similar to this
darklow/django-suit#337

this looks like duplicate: #27

This is almost certainly the same behavior I saw in #27. This issue much more clearly defined and better documented. I'd be happy to see mine (#27) closed and the progress tracked in this one. Any chance this might be fixed?