mirror of
https://github.com/django/django.git
synced 2025-07-06 18:59:13 +00:00
[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
This commit is contained in:
parent
23a3c08bbb
commit
a7c320fea7
@ -33,7 +33,7 @@ class FieldError(Exception):
|
||||
pass
|
||||
|
||||
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
|
||||
@ -63,3 +63,11 @@ class ValidationError(Exception):
|
||||
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
|
||||
|
||||
|
@ -9,7 +9,7 @@ from django.utils.datastructures import SortedDict
|
||||
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
|
||||
@ -254,10 +254,14 @@ class BaseModelForm(BaseForm):
|
||||
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):
|
||||
|
@ -1432,9 +1432,9 @@ False
|
||||
... exclude = ('quantity',)
|
||||
>>> form = PriceForm({'price': '6.00'})
|
||||
>>> form.is_valid()
|
||||
False
|
||||
>>> 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):
|
||||
|
Loading…
x
Reference in New Issue
Block a user