Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #268 -- Added AnyValidator and fixed small bug in [399]. Thanks…

…, Hugo

git-svn-id: http://code.djangoproject.com/svn/django/trunk@402 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 2315ccc4998087463310b1dc2cd3fed21685b5fd 1 parent dae5863
Adrian Holovaty authored August 04, 2005

Showing 1 changed file with 24 additions and 1 deletion. Show diff stats Hide diff stats

  1. 25  django/core/validators.py
25  django/core/validators.py
@@ -344,7 +344,30 @@ def __init__(self, regexp, error_message="The format for this field is wrong."):
344 344
 
345 345
     def __call__(self, field_data, all_data):
346 346
         if not self.regexp.match(field_data):
347  
-            raise validators.ValidationError(self.error_message)
  347
+            raise ValidationError(self.error_message)
  348
+
  349
+class AnyValidator:
  350
+    """
  351
+    This validator tries all given validators. If any one of them succeeds,
  352
+    validation passes. If none of them succeeds, the given message is thrown
  353
+    as a validation error. The message is rather unspecific, so it's best to
  354
+    specify one on instantiation.
  355
+    """
  356
+    def __init__(self, validator_list=[], error_message="This field is invalid."):
  357
+        self.validator_list = validator_list
  358
+        self.error_message = error_message
  359
+        for v in validator_list:
  360
+            if hasattr(v, 'always_test'):
  361
+                self.always_test = True
  362
+
  363
+    def __call__(self, field_data, all_data):
  364
+        for v in self.validator_list:
  365
+            try:
  366
+                v(field_data, all_data)
  367
+                return
  368
+            except ValidationError, e:
  369
+                pass
  370
+        raise ValidationError(self.error_message)
348 371
 
349 372
 class URLMimeTypeCheck:
350 373
     "Checks that the provided URL points to a document with a listed mime type"

0 notes on commit 2315ccc

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