Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Refactored django.core.validators.RequiredIfOtherFieldGiven to remove…

… duplicate code

git-svn-id: http://code.djangoproject.com/svn/django/trunk@403 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 0c6019295a86140fd231bccabc987496f23393eb 1 parent 2315ccc
@adrianholovaty adrianholovaty authored
Showing with 8 additions and 14 deletions.
  1. +8 −14 django/core/validators.py
View
22 django/core/validators.py
@@ -210,16 +210,6 @@ def __call__(self, field_data, all_data):
if field_data != all_data[self.other]:
raise ValidationError, self.error_message
-class RequiredIfOtherFieldGiven:
- def __init__(self, other_field_name, error_message=None):
- self.other = other_field_name
- self.error_message = error_message or "Please enter both fields or leave them both empty."
- self.always_test = True
-
- def __call__(self, field_data, all_data):
- if all_data[self.other] and not field_data:
- raise ValidationError, self.error_message
-
class RequiredIfOtherFieldNotGiven:
def __init__(self, other_field_name, error_message=None):
self.other = other_field_name
@@ -231,17 +221,21 @@ def __call__(self, field_data, all_data):
raise ValidationError, self.error_message
class RequiredIfOtherFieldsGiven:
- "Like RequiredIfOtherFieldGiven, but takes a list of required field names instead of a single field name"
- def __init__(self, other_field_names, error_message=None):
+ def __init__(self, other_field_names, error_message="Please enter both fields or leave them both empty."):
self.other = other_field_names
- self.error_message = error_message or "Please enter both fields or leave them both empty."
+ self.error_message = error_message
self.always_test = True
def __call__(self, field_data, all_data):
for field in self.other:
- if all_data.has_key(field) and all_data[field] and not field_data:
+ if all_data.get(field) and not field_data:
raise ValidationError, self.error_message
+class RequiredIfOtherFieldGiven(RequiredIfOtherFieldsGiven):
+ "Like RequiredIfOtherFieldsGiven, but takes a single field name instead of a list."
+ def __init__(self, other_field_name, error_message="Please enter both fields or leave them both empty."):
+ RequiredIfOtherFieldsGiven.__init__(self, [other_field_name], error_message)
+
class RequiredIfOtherFieldEquals:
def __init__(self, other_field, other_value, error_message=None):
self.other_field = other_field
Please sign in to comment.
Something went wrong with that request. Please try again.