Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
Adrian Holovaty authored August 04, 2005

Showing 1 changed file with 8 additions and 14 deletions. Show diff stats Hide diff stats

  1. 22  django/core/validators.py
22  django/core/validators.py
@@ -210,16 +210,6 @@ def __call__(self, field_data, all_data):
210 210
         if field_data != all_data[self.other]:
211 211
             raise ValidationError, self.error_message
212 212
 
213  
-class RequiredIfOtherFieldGiven:
214  
-    def __init__(self, other_field_name, error_message=None):
215  
-        self.other = other_field_name
216  
-        self.error_message = error_message or "Please enter both fields or leave them both empty."
217  
-        self.always_test = True
218  
-
219  
-    def __call__(self, field_data, all_data):
220  
-        if all_data[self.other] and not field_data:
221  
-            raise ValidationError, self.error_message
222  
-
223 213
 class RequiredIfOtherFieldNotGiven:
224 214
     def __init__(self, other_field_name, error_message=None):
225 215
         self.other = other_field_name
@@ -231,17 +221,21 @@ def __call__(self, field_data, all_data):
231 221
             raise ValidationError, self.error_message
232 222
 
233 223
 class RequiredIfOtherFieldsGiven:
234  
-    "Like RequiredIfOtherFieldGiven, but takes a list of required field names instead of a single field name"
235  
-    def __init__(self, other_field_names, error_message=None):
  224
+    def __init__(self, other_field_names, error_message="Please enter both fields or leave them both empty."):
236 225
         self.other = other_field_names
237  
-        self.error_message = error_message or "Please enter both fields or leave them both empty."
  226
+        self.error_message = error_message
238 227
         self.always_test = True
239 228
 
240 229
     def __call__(self, field_data, all_data):
241 230
         for field in self.other:
242  
-            if all_data.has_key(field) and all_data[field] and not field_data:
  231
+            if all_data.get(field) and not field_data:
243 232
                 raise ValidationError, self.error_message
244 233
 
  234
+class RequiredIfOtherFieldGiven(RequiredIfOtherFieldsGiven):
  235
+    "Like RequiredIfOtherFieldsGiven, but takes a single field name instead of a list."
  236
+    def __init__(self, other_field_name, error_message="Please enter both fields or leave them both empty."):
  237
+        RequiredIfOtherFieldsGiven.__init__(self, [other_field_name], error_message)
  238
+
245 239
 class RequiredIfOtherFieldEquals:
246 240
     def __init__(self, other_field, other_value, error_message=None):
247 241
         self.other_field = other_field

0 notes on commit 0c60192

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