#14420: Can't do validation on ModelFormSets to prevent deletion when there is only one object in formset and it is to be deleted ---------------------------------------------+------------------------------ Reporter: rory | Owner: nobody Status: new | Milestone: Component: Forms | Version: 1.1 Keywords: validation,formset,modelformset | Stage: Unreviewed Has_patch: 0 | ---------------------------------------------+------------------------------ I'm using a ModelFormSet (using modelformset_factory and a custom formset param) to change and delete objects. I want to do some validation, to prevent objects being deleted if they match certain criteria. I put this validation code in the clean() method of the custom formset object (which is a subclass of BaseModelFormSet). It loops over self.deleted_forms and raises a ValidationError if any of the forms to be deleted matches this criteria.
However this doesn't work if there is only one object/form in the formset, and you want to delete that one object, and that object should fail your 'don't delete objects like this' test. The FormSet code shortcircuits the validation (since you should be allowed to delete an object without making it valid first), and doesn't call self.errors (on the FormSet object), and doesn't call the (custom) clean() method. The is_valid() method of the (bound) FormSet returns True, when it should return False. The non_form_errors() returns an empty list, when it should have the errors saying that you can't delete this object. You can then .save() this formset and delete the object that you shouldn't be able to delete. A simple work around I am using is to call formset.full_clean() just after initialising the formset (i.e. before calling formset.is_valid()). This calls all the clean() methods of all the forms and of the FormSet. -- Ticket URL: <http://code.djangoproject.com/ticket/14420> Django <http://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To post to this group, send email to django-upda...@googlegroups.com. To unsubscribe from this group, send email to django-updates+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-updates?hl=en.