Opened 18 years ago

Closed 18 years ago

Last modified 18 years ago

#3284 closed defect (worksforme)

Form clean() method does not trap ValidationError exception

Reported by: Chris Rose <no-spam@…> Owned by: Adrian Holovaty
Component: Forms Version:
Severity: normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

class CreateUserForm(forms.Form):
    username = forms.CharField(max_length=20)
    email = forms.EmailField()
    password = forms.CharField(widget=forms.PasswordInput)
    password2 = forms.CharField(widget=forms.PasswordInput, label="Confirm Password")
    
    def clean(self):
        if self.clean_data.has_key('password') and self.clean_data.has_key('password2'):
            if self.clean_data['password'] != self.clean_data['password2']:
                raise ValidationError("The passwords you have entered do not match.")
        return self.clean_data

When the above form is validated, if both password fields are set but do not match the ValidationError exception does not get caught by BaseForm.full_clean() as I think it should. The ValidationError should make its way into the form level error list, but instead all you get is a traceback.

ValidationError at /users/new/
[u'The passwords you have entered do not match.']
Request Method: 	POST
Request URL: 	http://localhost:8000/users/new/
Exception Type: 	ValidationError
Exception Value: 	[u'The passwords you have entered do not match.']
Exception Location: 	/home/devel/jewel/../jewel/user/views.py in clean, line 32

Change History (2)

comment:1 by Adrian Holovaty, 18 years ago

Resolution: worksforme
Status: newclosed

We have a unit test that confirms this works properly:

http://code.djangoproject.com/browser/django/trunk/tests/regressiontests/forms/tests.py?rev=4306#L1966

I suspect you're raising the wrong ValidationError exception. Make sure to use django.newforms.ValidationError rather than the old one, django.core.validators.ValidationError.

comment:2 by Chris Rose <no-spam@…>, 18 years ago

Now I feel stupid :-)

Thats what it was, sorry for the noise.

Note: See TracTickets for help on using tickets.
Back to Top