Search code examples
pythondjangoformset

Formset invalid but 'Save' saves parent model and closes page


I have a formset payment on a parent model invoice. If the user enters a payment but forget the payment date (which is required) the page still saves the invoice and then closes.

I can see from form_valid that formset.is_valid() is False. I can see the error raised if I print it in console: [{}, {}, {'paymentdate': ['This field is required.']}, {}]. I can even see the validation show up on the page right before is closes.

How do I keep the page from closing when there is a validation error?


Solution

  • However, form fields of formsets won’t include the required attribute as that validation may be incorrect when adding and deleting forms. https://docs.djangoproject.com/en/3.0/topics/forms/formsets/

    Formsets are very limited in capabilities and resource inefficient, so it's best not to use them. You'd have to overwrite your view to customize the workflow.