Author: Honza_Kral
Date: 2009-06-17 19:31:57 -0500 (Wed, 17 Jun 2009)
New Revision: 11035

Modified:
   django/branches/soc2009/model-validation/django/core/exceptions.py
   django/branches/soc2009/model-validation/django/core/validators.py
   django/branches/soc2009/model-validation/django/forms/fields.py
Log:
[soc2009/model-validation] Added code param to ValidationError and use it to 
override validator's messages

Modified: django/branches/soc2009/model-validation/django/core/exceptions.py
===================================================================
--- django/branches/soc2009/model-validation/django/core/exceptions.py  
2009-06-18 00:31:37 UTC (rev 11034)
+++ django/branches/soc2009/model-validation/django/core/exceptions.py  
2009-06-18 00:31:57 UTC (rev 11035)
@@ -35,7 +35,7 @@
 NON_FIELD_ERRORS = '__all__'
 class ValidationError(Exception):
     """An error while validating data."""
-    def __init__(self, message):
+    def __init__(self, message, code=None):
         import operator
         from django.utils.encoding import force_unicode
         """
@@ -49,11 +49,10 @@
         if isinstance(message, list):
             self.messages = [force_unicode(msg) for msg in message]
         else:
+            self.code = code
             message = force_unicode(message)
             self.messages = [message]
 
-
-
     def __str__(self):
         # This is needed because, without a __str__(), printing an exception
         # instance would result in this:

Modified: django/branches/soc2009/model-validation/django/core/validators.py
===================================================================
--- django/branches/soc2009/model-validation/django/core/validators.py  
2009-06-18 00:31:37 UTC (rev 11034)
+++ django/branches/soc2009/model-validation/django/core/validators.py  
2009-06-18 00:31:57 UTC (rev 11035)
@@ -20,7 +20,7 @@
 
 def validate_email(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):
     def get_value(self, name, all_values, obj):

Modified: django/branches/soc2009/model-validation/django/forms/fields.py
===================================================================
--- django/branches/soc2009/model-validation/django/forms/fields.py     
2009-06-18 00:31:37 UTC (rev 11034)
+++ django/branches/soc2009/model-validation/django/forms/fields.py     
2009-06-18 00:31:57 UTC (rev 11035)
@@ -131,7 +131,10 @@
                 try:
                     v(value)
                 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:
             raise ValidationError(errors)
 


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to django-updates@googlegroups.com
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to