Maddie Graham
Maddie Graham

Reputation: 2177

Validators do not work as they should. Django

I am trying to add a validator in accordance with the Django documentation to my field but I can not get the effect, every time i can sent e-mail even though there is no "[email protected]" in the field.

Do I have to add any additional lines of code in the view? any help will be appreciated.

class EmailContactForm(forms.Form):
    e_mail = forms.EmailField()
    tresc_wiadomosci = forms.CharField(widget=forms.Textarea)

    def clean_recipients(self):
        data = self.cleaned_data['e_mail']
        if "[email protected]" not in data:
            raise forms.ValidationError("You have forgotten about Fred!")

Upvotes: 0

Views: 40

Answers (2)

Jeanpierre Rivas
Jeanpierre Rivas

Reputation: 282

change clean_recipients to clean_e_mail

class EmailContactForm(forms.Form):
e_mail = forms.EmailField()
tresc_wiadomosci = forms.CharField(widget=forms.Textarea)

def clean_e_mail(self):
    data = self.cleaned_data['e_mail']
    if "[email protected]" not in data:
        raise forms.ValidationError("You have forgotten about Fred!")

Upvotes: 2

Sam
Sam

Reputation: 2084

If you want to override the clean method of a form, you need to give it the same name as the field it cleans. In this case, you should name your method clean_e_mail.

Upvotes: 0

Related Questions