#3284 closed defect (worksforme)
Form clean() method does not trap ValidationError exception
| Reported by: | 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
Note:
See TracTickets
for help on using tickets.
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
ValidationErrorexception. Make sure to usedjango.newforms.ValidationErrorrather than the old one,django.core.validators.ValidationError.