Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[soc2009/model-validation] Raise UnresolvableValidationError if an er…

…ror occurs on excluded field

git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/model-validation@12072 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit a7c320fea79b8750ab2a8aca84d8e8fb76d8b100 1 parent 23a3c08
Honza Král authored January 04, 2010
10  django/core/exceptions.py
@@ -33,7 +33,7 @@ class FieldError(Exception):
33 33
     pass
34 34
 
35 35
 NON_FIELD_ERRORS = '__all__'
36  
-class ValidationError(Exception):
  36
+class BaseValidationError(Exception):
37 37
     """An error while validating data."""
38 38
     def __init__(self, message, code=None, params=None):
39 39
         import operator
@@ -63,3 +63,11 @@ def __str__(self):
63 63
         if hasattr(self, 'message_dict'):
64 64
             return repr(self.message_dict)
65 65
         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
+
8  django/forms/models.py
@@ -9,7 +9,7 @@
9 9
 from django.utils.text import get_text_list, capfirst
10 10
 from django.utils.translation import ugettext_lazy as _, ugettext
11 11
 
12  
-from django.core.exceptions import ValidationError, NON_FIELD_ERRORS
  12
+from django.core.exceptions import ValidationError, NON_FIELD_ERRORS, UnresolvableValidationError
13 13
 from django.core.validators import EMPTY_VALUES
14 14
 from util import ErrorList
15 15
 from forms import BaseForm, get_declared_fields
@@ -254,10 +254,14 @@ def clean(self):
254 254
                     if k in self.cleaned_data:
255 255
                         del self.cleaned_data[k]
256 256
 
257  
-            # what about fields that don't validate but aren't present on the form?
258 257
             if NON_FIELD_ERRORS in e.message_dict:
259 258
                 raise ValidationError(e.message_dict[NON_FIELD_ERRORS])
260 259
 
  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
+
261 265
         return self.cleaned_data
262 266
 
263 267
     def save(self, commit=True):
6  tests/modeltests/model_forms/models.py
@@ -1432,9 +1432,9 @@ def __unicode__(self):
1432 1432
 ...         exclude = ('quantity',)
1433 1433
 >>> form = PriceForm({'price': '6.00'})
1434 1434
 >>> 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.']}
1438 1438
 
1439 1439
 # Unique & unique together with null values
1440 1440
 >>> class BookForm(ModelForm):

0 notes on commit a7c320f

Please sign in to comment.
Something went wrong with that request. Please try again.