jazzband / django-formtools

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

Added JQuery to skip validation when navigating to previous form #81

Closed J20S closed 7 years ago

J20S commented 8 years ago

Hi,

In wizard form, the current form will be forced to validate when you want to navigate to one of the previous forms. I personally think this is not the expected feature by users (at least not a user-friendly feature).

I see in wizard/views.py,

def post(self, *args, **kwargs):
    """
    This method handles POST requests.

    The wizard will render either the current step (if form validation
    wasn't successful), the next step (if the current step was stored
    successful) or the done view (if no more steps are available)
    """
    # Look for a wizard_goto_step element in the posted data which
    # contains a valid step name. If one was found, render the requested
    # form. (This makes stepping back a lot easier).
    wizard_goto_step = self.request.POST.get('wizard_goto_step', None)
    if wizard_goto_step and wizard_goto_step in self.get_form_list():
        return self.render_goto_step(wizard_goto_step)
    ...

the logic is to render previous form for user before doing any further validation check.

However, if a form has compulsory fields, client-side JavaScript / JQuery will actually prevent the user from submitting the form. In other words, the form has to pass basic client-side validations to be able to hit this function.

My proposed solution is also to use JQuery or Javascript to add the expected attribute "wizard_goto_step" to the form in the template. I also feel like there is a need to modify the default template wizard_form.html as well so that new users won't get confused. This is the whole idea of this pull request.

Cheers, James

timgraham commented 7 years ago

The good news is that you can do this by using a custom template without patching django-formtools at all. I think that's the way to go.