elo80ka / django-dynamic-formset

A jQuery plugin that allows you dynamically add new forms to a rendered django formset.
677 stars 311 forks source link

Mark hidden last form as deleted to prevent validation #174

Open fkdeboer opened 4 years ago

fkdeboer commented 4 years ago

Using inline formsets, if hideLastAddForm is set to true, and the formset is saved without adding a first form, clean() is still called on the hidden form, which can lead to form errors which are not shown. I would suggest something like the following, which works in my specific:

if (options.hideLastAddForm) {
                   var row= $('.' + options.formCssClass + ':last');
                    var del = row.find('input:hidden[id $= "-DELETE"]');
                    del.val('on');
                    row.hide();
 }

moreover, to prevent hiding the last form when form validation fails, in the template I have to set the hideLastAddForm only for the GET method: {% if request.method == "GET" %} hideLastAddForm: true, {% endif %}

Unfortunately, my knowledge of the relevant use cases for django-dynamic-formset (and JS in general) is insufficient to open a pull request..

guma44 commented 3 years ago

@fkdeboer Thanks for the hint. It was really useful.