Anonymous committed 66318a6

[soc2009/model-validation] Raise UnresolvableValidationError if an error occurs on excluded field

  • Participants
  • Parent commits cb59c4d
  • Branches soc2009/model-validation

Comments (0)

Files changed (3)

File django/core/

 NON_FIELD_ERRORS = '__all__'
-class ValidationError(Exception):
+class BaseValidationError(Exception):
     """An error while validating data."""
     def __init__(self, message, code=None, params=None):
         import operator
         if hasattr(self, 'message_dict'):
             return repr(self.message_dict)
         return repr(self.messages)
+class ValidationError(BaseValidationError):
+    pass
+class  UnresolvableValidationError(BaseValidationError):
+    """Validation error that cannot be resolved by the user."""
+    pass

File django/forms/

 from django.utils.text import get_text_list, capfirst
 from django.utils.translation import ugettext_lazy as _, ugettext
-from django.core.exceptions import ValidationError, NON_FIELD_ERRORS
+from django.core.exceptions import ValidationError, NON_FIELD_ERRORS, UnresolvableValidationError
 from django.core.validators import EMPTY_VALUES
 from util import ErrorList
 from forms import BaseForm, get_declared_fields
                     if k in self.cleaned_data:
                         del self.cleaned_data[k]
-            # what about fields that don't validate but aren't present on the form?
             if NON_FIELD_ERRORS in e.message_dict:
                 raise ValidationError(e.message_dict[NON_FIELD_ERRORS])
+            # there are errors on some fields not displayed in this form
+            if set(e.message_dict.keys()) - set(self.fields.keys() + [NON_FIELD_ERRORS]):
+                raise UnresolvableValidationError(e.message_dict)
         return self.cleaned_data
     def save(self, commit=True):

File tests/modeltests/model_forms/

 ...         exclude = ('quantity',)
 >>> form = PriceForm({'price': '6.00'})
 >>> form.is_valid()
->>> form.errors
-{'quantity': [u'This field cannot be null.']}
+Traceback (most recent call last):
+  ...
+UnresolvableValidationError: {'quantity': [u'This field cannot be null.']}
 # Unique & unique together with null values
 >>> class BookForm(ModelForm):