如何删除django crispy表单字段验证错误?

时间:2017-11-17 06:02:35

标签: django django-crispy-forms

我想覆盖crispy表单中的错误以隐藏默认的crispy表单字段(用户名)验证错误,因为我已经使用了clean方法进行验证。

Image

class AddEmployeeForm(ModelForm):
    password = forms.CharField(widget=forms.PasswordInput)

    def __init__(self, *args, **kwargs):
        super(AddEmployeeForm, self).__init__(*args, **kwargs)

        for fieldname in ['username', ]:
            self.fields[fieldname].help_text = None

        for key in self.fields:
            self.fields[key].required = True

        self.helper = FormHelper(self)
        self.helper.form_method = 'POST'
        self.helper.form_action = '/add_employee/'
        self.helper.form_class = 'form-horizontal'
        self.helper.form_show_errors = True
        self.helper.error_text_inline = False
        # NEW:
        self.helper.label_class = 'col-sm-4'
        self.helper.field_class = 'col-sm-5'

        self.helper.layout = Layout(
            Field('first_name', css_class='input-sm'),
            Field('last_name', css_class='input-sm'),
            Field('username', css_class='input-sm'),
            Field('email', css_class='input-sm'),
            Field('password', css_class='input-sm'),
            # NEW:
            ButtonHolder(
                Submit('submit', 'Submit', css_class='btn btn-primary')
            )
        )

    class Meta:
        model = User
        fields = ['first_name', 'last_name', 'username', 'email', 'password']

    def clean(self):
        cleaned_data = super(AddEmployeeForm, self).clean()
        email = cleaned_data.get("email")
        username = cleaned_data.get("username")
        if User.objects.filter(email=email):
            raise forms.ValidationError('Email already exists.')
        if User.objects.filter(username=username):
            raise forms.ValidationError('Username already exists.')
        return self.cleaned_data

在模板中我只使用了它。:

{% load crispy_forms_tags %}

{% crispy form %}

1 个答案:

答案 0 :(得分:0)

请将form_show_errors参数设置为helper更改为False

即。替换

self.helper.form_show_errors = True

self.helper.form_show_errors = False

您的新AddEmployeeForm课程如下所示

class AddEmployeeForm(ModelForm):
    password = forms.CharField(widget=forms.PasswordInput)

    def __init__(self, *args, **kwargs):
        super(AddEmployeeForm, self).__init__(*args, **kwargs)

        for fieldname in ['username', ]:
            self.fields[fieldname].help_text = None

        for key in self.fields:
            self.fields[key].required = True

        self.helper = FormHelper(self)
        self.helper.form_method = 'POST'
        self.helper.form_action = '/add_employee/'
        self.helper.form_class = 'form-horizontal'
        self.helper.form_show_errors = False
        self.helper.error_text_inline = False
        # NEW:
        self.helper.label_class = 'col-sm-4'
        self.helper.field_class = 'col-sm-5'

        self.helper.layout = Layout(
            Field('first_name', css_class='input-sm'),
            Field('last_name', css_class='input-sm'),
            Field('username', css_class='input-sm'),
            Field('email', css_class='input-sm'),
            Field('password', css_class='input-sm'),
            # NEW:
            ButtonHolder(
                Submit('submit', 'Submit', css_class='btn btn-primary')
            )
        )

    class Meta:
        model = User
        fields = ['first_name', 'last_name', 'username', 'email', 'password']

    def clean(self):
        cleaned_data = super(AddEmployeeForm, self).clean()
        email = cleaned_data.get("email")
        username = cleaned_data.get("username")
        if User.objects.filter(email=email):
            raise forms.ValidationError('Email already exists.')
        if User.objects.filter(username=username):
            raise forms.ValidationError('Username already exists.')
        return self.cleaned_data