Search code examples
djangovalidationdjango-formwizard

How to disable fields validation if we want to go to the previous step?


I am using the django's form wizard in the authentication system. I have 2 form wizards steps. In the second, I have two buttons: the Prev Step button and the Submit button. When the Prev Step button is used to go backwards, I get the django's warning to fill out the field.

How do I disable validations for the Prev Step button and keep the validations for the Submit button?

I already disabled the javascript validations for the Prev Step button.

My html:

<!-- Forms -->
{{ wizard.management_form }}
{% for field in wizard.form %}
  <div class="wrap-input100 rs1-wrap-input100 validate-input m-b-20">
    {{ field }}
    <span class="focus-input100"></span>
  </div>
{% endfor %}

<!-- Buttons -->
{% if wizard.steps.next %}
  <div class="container-login100-form-btn">
    <button type="submit" value="{{ wizard.steps.next }}" class="login100-form-btn">Next step</button>
  </div>
{% else %}
  <div class="container-login100-form-btn">
    <div class="split-left">
      <button type="submit" value="{{ wizard.steps.prev }}" class="login100-form-btn" formnovalidate>Prev step</button>
    </div>
    <div class="split-right">
        <button type="submit" class="login100-form-btn">Sign up</button>
    </div>
  </div>
{% endif %}

views:

class signup(SessionWizardView):
    template_name='accounts/signup.html'
    form_list = [UserCreationForm_1, UserCreationForm_2]

    def done(self, form_list, **kwargs):

        form_step = [form for form in form_list]

        # step 1: ModelForm
        user = form_step[0].save()
        auth_login( self.request, user )
        # step 2: Form
        user = form_step[1].save()

        return redirect( 'home' )

forms:

class UserCreationForm_1(forms.ModelForm):
    password1 = forms.CharField(widget=forms.PasswordInput(attrs={'class':'input100', 'placeholder': 'Password'}))
    password2 = forms.CharField(widget=forms.PasswordInput(attrs={'class':'input100', 'placeholder': 'Repeat Password'}))

    class Meta:
        model = MyUser
        fields = ('shown_name', 'email')
        widgets = {
            'email': forms.EmailInput(attrs={'class':'input100', 'placeholder': 'Email'}),
            'shown_name': forms.TextInput(attrs={'class':'input100', 'placeholder': 'User name'}),
        }

class UserCreationForm_2(forms.Form):
    name = forms.CharField(  max_length=40, widget=forms.TextInput(attrs={'class':'input100', 'placeholder': 'Name'}) )
    date_of_birth = forms.DateField( widget=forms.DateInput(attrs={'class':'input100', 'placeholder': 'Date of Birth'}) )
    phone = PhoneNumberField( widget=forms.TextInput(attrs={'class':'input100', 'placeholder': 'Phone'}) )

In the second form wizard step, when I use the Prev button, this happens: https://i.sstatic.net/PnDHt.jpg
When I use the Submit button, this happens: https://i.sstatic.net/OrEml.jpg


Solution

  • The problem had nothing to do with Django. It was a javascript problem. I only had to remove the class validate-input from the forms' div.