如何验证django表单中的多个字段和一个额外的自定义清理功能?

时间:2013-03-08 22:24:20

标签: python django

验证单个字段是没有问题的。 self.cleaned_data显示值!

但是当我想在清理方法中打印self.cleaned_data时,所有值的结果都是None!怎么会发生?我也尝试用Super()获取数据......但没有运气...... 有任何想法吗?

def validator_number_or_one_letter(fieldvalue):
    if not re.match("^[A-Za-z0-9]*$", fieldvalue) or len(fieldvalue)>1:

        raise forms.ValidationError("Error, Error!")


class CodeForm(forms.Form):

    letter_one = forms.CharField(widget=forms.TextInput(attrs={'placeholder': '#','requiried':'required','required': 'true','size':'1','class':'first-field' }))
    letter_two =forms.CharField(widget=forms.TextInput(attrs={'placeholder': '#','requiried':'required','required': 'true','size':'1' }))
    letter_three = forms.CharField(widget=forms.TextInput(attrs={'placeholder': '#', 'requiried':'required','required': 'true','size':'1' }))
    letter_four = forms.CharField(widget=forms.TextInput(attrs={'placeholder': '#','requiried':'required','required': 'true','size':'1' }))
    letter_five = forms.CharField(widget=forms.TextInput(attrs={'placeholder': '#','requiried':'required','required': 'true','size':'1' }))

    def clean(self):
        #cleaned_data = super(CodeForm, self).clean()  ###this does not work either
        #print cleaned_data ###this does not work either
        print self.cleaned_data['letter_one']

    def clean_letter_one(self):
        validator_number_or_one_letter(self.cleaned_data['letter_one'])

    def clean_letter_two(self):
        validator_number_or_one_letter(self.cleaned_data['letter_two'])

    def clean_letter_three(self):
        validator_number_or_one_letter(self.cleaned_data['letter_three'])

    def clean_letter_four(self):
        validator_number_or_one_letter(self.cleaned_data['letter_four'])

    def clean_letter_five(self):
        validator_number_or_one_letter(self.cleaned_data['letter_five']) 

1 个答案:

答案 0 :(得分:0)

不确定这是否是唯一的问题,但是clean_()方法必须返回已清理的值。