jazzband / django-formtools

A set of high-level abstractions for Django forms
https://django-formtools.readthedocs.io
BSD 3-Clause "New" or "Revised" License
784 stars 135 forks source link

WizardForm should have a `form_invalid()` method #268

Open nerdoc opened 4 months ago

nerdoc commented 4 months ago

As it is perfectly fine to have no form_valid() method - WizardView provides a done() method for the final good end - there is no way to interact with the view if one form step was invalid, and I want to change the result.

My use case: I mix the forms with some mixins that provide dynamic HTMX updates - if one field is changed, this could trigger a reload of the whole form with different params (querysets, other fields etc) by HTMX. This must be done using POST, and therefore the form is validated, suddenly showing errors all over the place (empty fields etc).

I don't want that, so I changed the mixin class' form_invalid() method like this in my mixin:

def form_invalid(self, form):
    if self.request.POST.get("_dynamic_reload"):
        form.errors.clear()
    return super().form_invalid(form)

if there was sent a special parameter with the HTMX request, the form errors are cleared. This works reasonably well.

But I can't do that when I subclass WizardView. It could be solved by adding a WizardView.invalid_form() method, which is called instead of just rendering the form manually in post(), and could be overridden then in subclasses if needed.