Skip to content

Commit a7c320f

Browse files
committed
[soc2009/model-validation] Raise UnresolvableValidationError if an error occurs on excluded field
git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/model-validation@12072 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent 23a3c08 commit a7c320f

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

django/core/exceptions.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class FieldError(Exception):
3333
pass
3434

3535
NON_FIELD_ERRORS = '__all__'
36-
class ValidationError(Exception):
36+
class BaseValidationError(Exception):
3737
"""An error while validating data."""
3838
def __init__(self, message, code=None, params=None):
3939
import operator
@@ -63,3 +63,11 @@ def __str__(self):
6363
if hasattr(self, 'message_dict'):
6464
return repr(self.message_dict)
6565
return repr(self.messages)
66+
67+
class ValidationError(BaseValidationError):
68+
pass
69+
70+
class UnresolvableValidationError(BaseValidationError):
71+
"""Validation error that cannot be resolved by the user."""
72+
pass
73+

django/forms/models.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from django.utils.text import get_text_list, capfirst
1010
from django.utils.translation import ugettext_lazy as _, ugettext
1111

12-
from django.core.exceptions import ValidationError, NON_FIELD_ERRORS
12+
from django.core.exceptions import ValidationError, NON_FIELD_ERRORS, UnresolvableValidationError
1313
from django.core.validators import EMPTY_VALUES
1414
from util import ErrorList
1515
from forms import BaseForm, get_declared_fields
@@ -254,10 +254,14 @@ def clean(self):
254254
if k in self.cleaned_data:
255255
del self.cleaned_data[k]
256256

257-
# what about fields that don't validate but aren't present on the form?
258257
if NON_FIELD_ERRORS in e.message_dict:
259258
raise ValidationError(e.message_dict[NON_FIELD_ERRORS])
260259

260+
# there are errors on some fields not displayed in this form
261+
if set(e.message_dict.keys()) - set(self.fields.keys() + [NON_FIELD_ERRORS]):
262+
raise UnresolvableValidationError(e.message_dict)
263+
264+
261265
return self.cleaned_data
262266

263267
def save(self, commit=True):

tests/modeltests/model_forms/models.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1432,9 +1432,9 @@ def __unicode__(self):
14321432
... exclude = ('quantity',)
14331433
>>> form = PriceForm({'price': '6.00'})
14341434
>>> form.is_valid()
1435-
False
1436-
>>> form.errors
1437-
{'quantity': [u'This field cannot be null.']}
1435+
Traceback (most recent call last):
1436+
...
1437+
UnresolvableValidationError: {'quantity': [u'This field cannot be null.']}
14381438
14391439
# Unique & unique together with null values
14401440
>>> class BookForm(ModelForm):

0 commit comments

Comments
 (0)