Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error on raising ValidationError as dictionary

Django newbie here. I am trying to raise an error for email field in my custom form. My forms.py has following code to validate email:

def clean_email(self):
    email = self.cleaned_data["email"]
        try:
            User._default_manager.get(email=email)
                except User.DoesNotExist:
                return email
        raise ValueError({'email':'Email already registered. 
                     Login to continue or use another email.'})

On entering existing email again, I get following error on my debug screen of app:

enter image description here

What am I doing wrong here? I am following this LINK

EDIT Getting this error on changing ValueError to ValidationError The argument field must be None when the error argument contains errors for multiple fields.

like image 395
Maxsteel Avatar asked Jan 09 '23 06:01

Maxsteel


1 Answers

Use raise ValidationError instead of raise ValueError:

def clean(self):
    email = self.cleaned_data["email"]
    try:
        User._default_manager.get(email=email)
    except User.DoesNotExist:
        return self.cleaned_data
    raise ValidationError({'email':'Email already registered. Login to continue or use another email.'})
like image 123
Eugene Soldatov Avatar answered Jan 15 '23 12:01

Eugene Soldatov