如果要转到上一步,如何禁用字段验证?

时间:2019-01-06 18:49:28

标签: django validation django-formwizard

我正在身份验证系统中使用django的表单向导。 我有2个表单向导步骤。在第二个中,我有两个按钮:“上一步”按钮和“提交”按钮。当使用“上一步”按钮后退时,我收到django的警告,要求填写该字段。

如何禁用“上一步”按钮的验证并保留“提交”按钮的验证?

我已经禁用了“上一步”按钮的javascript验证。

我的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 %}

观看次数:

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' )

表格:

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'}) )

在第二个表单向导步骤中,当我使用Prev按钮时,会发生以下情况: https://imgur.com/54VvvZx
当我使用“提交”按钮时,会发生以下情况: https://imgur.com/JLCYMmr

1 个答案:

答案 0 :(得分:0)

该问题与Django无关。这是一个JavaScript问题。我只需要从表单的div中删除类validate-input。