summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian Holovaty <adrian@holovaty.com>2005-08-04 14:57:20 +0000
committerAdrian Holovaty <adrian@holovaty.com>2005-08-04 14:57:20 +0000
commit2315ccc4998087463310b1dc2cd3fed21685b5fd (patch)
tree1b75d3c4bcdf722e9abf704d940b8da73212faf2
parentdae5863058cc53278bd830be8e89492f62e46117 (diff)
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
-rw-r--r--django/core/validators.py25
1 files changed, 24 insertions, 1 deletions
diff --git a/django/core/validators.py b/django/core/validators.py
index 7e6f426278..b5901cbc3b 100644
--- a/django/core/validators.py
+++ b/django/core/validators.py
@@ -344,7 +344,30 @@ class MatchesRegularExpression:
def __call__(self, field_data, all_data):
if not self.regexp.match(field_data):
- raise validators.ValidationError(self.error_message)
+ raise ValidationError(self.error_message)
+
+class AnyValidator:
+ """
+ This validator tries all given validators. If any one of them succeeds,
+ validation passes. If none of them succeeds, the given message is thrown
+ as a validation error. The message is rather unspecific, so it's best to
+ specify one on instantiation.
+ """
+ def __init__(self, validator_list=[], error_message="This field is invalid."):
+ self.validator_list = validator_list
+ self.error_message = error_message
+ for v in validator_list:
+ if hasattr(v, 'always_test'):
+ self.always_test = True
+
+ def __call__(self, field_data, all_data):
+ for v in self.validator_list:
+ try:
+ v(field_data, all_data)
+ return
+ except ValidationError, e:
+ pass
+ raise ValidationError(self.error_message)
class URLMimeTypeCheck:
"Checks that the provided URL points to a document with a listed mime type"