mirror of
https://github.com/django/django.git
synced 2025-07-07 11:19:12 +00:00
[soc2009/model-validation] Added code param to ValidationError and use it to override validator's messages
git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/model-validation@11035 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
d707d37048
commit
ae581816be
@ -35,7 +35,7 @@ class FieldError(Exception):
|
|||||||
NON_FIELD_ERRORS = '__all__'
|
NON_FIELD_ERRORS = '__all__'
|
||||||
class ValidationError(Exception):
|
class ValidationError(Exception):
|
||||||
"""An error while validating data."""
|
"""An error while validating data."""
|
||||||
def __init__(self, message):
|
def __init__(self, message, code=None):
|
||||||
import operator
|
import operator
|
||||||
from django.utils.encoding import force_unicode
|
from django.utils.encoding import force_unicode
|
||||||
"""
|
"""
|
||||||
@ -49,11 +49,10 @@ class ValidationError(Exception):
|
|||||||
if isinstance(message, list):
|
if isinstance(message, list):
|
||||||
self.messages = [force_unicode(msg) for msg in message]
|
self.messages = [force_unicode(msg) for msg in message]
|
||||||
else:
|
else:
|
||||||
|
self.code = code
|
||||||
message = force_unicode(message)
|
message = force_unicode(message)
|
||||||
self.messages = [message]
|
self.messages = [message]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
# This is needed because, without a __str__(), printing an exception
|
# This is needed because, without a __str__(), printing an exception
|
||||||
# instance would result in this:
|
# instance would result in this:
|
||||||
|
@ -20,7 +20,7 @@ email_re = re.compile(
|
|||||||
|
|
||||||
def validate_email(value):
|
def validate_email(value):
|
||||||
if not email_re.search(smart_unicode(value)):
|
if not email_re.search(smart_unicode(value)):
|
||||||
raise ValidationError(_(u'Enter a valid e-mail address.'))
|
raise ValidationError(_(u'Enter a valid e-mail address.'), code='invalid')
|
||||||
|
|
||||||
class ComplexValidator(object):
|
class ComplexValidator(object):
|
||||||
def get_value(self, name, all_values, obj):
|
def get_value(self, name, all_values, obj):
|
||||||
|
@ -131,7 +131,10 @@ class Field(object):
|
|||||||
try:
|
try:
|
||||||
v(value)
|
v(value)
|
||||||
except ValidationError, e:
|
except ValidationError, e:
|
||||||
errors.extend(e.messages)
|
if hasattr(e, 'code'):
|
||||||
|
errors.append(self.error_messages.get(e.code, e.messages[0]))
|
||||||
|
else:
|
||||||
|
errors.extend(e.messages)
|
||||||
if errors:
|
if errors:
|
||||||
raise ValidationError(errors)
|
raise ValidationError(errors)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user