Django form validation don't function

I try to put validations in my form but after I follow the official documentation and tutorials I don't obtain the solution. This is my code: (The variables of class are iddented)

class QuestionForm(forms.Form):
subject = forms.CharField(widget=forms.Select(attrs={'class': 'select'},
                          choices=SUBJECTS_SELE),
                          label='Materia')
question = forms.CharField(widget=forms.Textarea(attrs={'class': 'form-control', 'rows': 1}),
                           label='Pregunta',
                           max_length=5000)
answer = forms.CharField(widget=forms.Textarea(attrs={'class': 'form-control', 'rows': 1}),
                         label='Respuesta correcta',
                         max_length=1000)
answerOne = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control', 'rows': 1}),
                            label='Respuesta incorrecta',
                            max_length=1000,
                            required=True)

def clean_question(self):
     question = self.cleaned_data.get('question')
     a = "0"
     if not a in question:
         raise forms.ValidationError("Es obligatorio llenar este campo")
     return question


def clean_answer(self):
     answer = self.cleaned_data.get('answer')
     a = "0"
     if not a in answer:
         raise forms.ValidationError("Es obligatorio llenar este campo")
     return answer

Also I have try this way:

def clean(self, *args, **kwargs):
        question = self.cleaned_data.get('question')
        answer = self.cleaned_data.get('answer')
        if question != "0":
            raise forms.ValidationError("Es obligatorio llenar este campo")
        if answer != "0":
            raise forms.ValidationError("Es obligatorio llenar este campo")
        return super(QuestionForm, self).clean(*args, **kwargs)

Can you help me, thanks.

2 answers

  • answered 2017-11-15 01:06 Ykh

    class QuestionForm(forms.Form):
        subject = forms.CharField(widget=forms.Select(attrs={'class': 'select'},
                              choices=SUBJECTS_SELE),
                              label='Materia')
        question = forms.CharField(widget=forms.Textarea(attrs={'class': 'form-control', 'rows': 1}),
                               label='Pregunta',
                               max_length=5000)
        answer = forms.CharField(widget=forms.Textarea(attrs={'class': 'form-control', 'rows': 1}),
                             label='Respuesta correcta',
                             max_length=1000)
        answerOne = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control', 'rows': 1}),
                                label='Respuesta incorrecta',
                                max_length=1000,
                                required=True)
    
        def clean_question(self):
            question = self.cleaned_data.get('question')
            a = "0"
            if a not in question:
                raise forms.ValidationError("Es obligatorio llenar este campo")
            return question
    
    
        def clean_answer(self):
            answer = self.cleaned_data.get('answer')
            a = "0"
            if a not in answer:
                raise forms.ValidationError("Es obligatorio llenar este campo")
            return answer
    

  • answered 2017-11-15 05:45 Saranya Sridharan

    I believe when the user did not give the value in the must field Question or answer, it will be empty , so below code should work.

    class QuestionForm(forms.Form):
        subject = forms.CharField(widget=forms.Select(attrs={'class': 'select'},
                              choices=SUBJECTS_SELE),
                              label='Materia')
        question = forms.CharField(widget=forms.Textarea(attrs={'class': 'form-control', 'rows': 1}),
                               label='Pregunta',
                               max_length=5000)
        answer = forms.CharField(widget=forms.Textarea(attrs={'class': 'form-control', 'rows': 1}),
                             label='Respuesta correcta',
                             max_length=1000)
        answerOne = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control', 'rows': 1}),
                                label='Respuesta incorrecta',
                                max_length=1000,
                                required=True)
    
        def clean_question(self):
            question = self.cleaned_data.get('question')
    
            if not question:
                raise forms.ValidationError("Es obligatorio llenar este campo")
            return question
    
    
        def clean_answer(self):
            answer = self.cleaned_data.get('answer')
    
            if not answer:
                raise forms.ValidationError("Es obligatorio llenar este campo")
            return answer