Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #2104: allow somewhat nicer error messages in RequiredIfOtherFi…

…eldEquals validator. Thanks, Steven Armstrong

git-svn-id: http://code.djangoproject.com/svn/django/trunk@4577 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit cef22390862a2ad9ecdfbe3fabc74fbd81160b39 1 parent 5b66c74
Jacob Kaplan-Moss authored February 25, 2007
10  django/core/validators.py
@@ -312,11 +312,12 @@ def __init__(self, other_field_name, error_message=gettext_lazy("Please enter bo
312 312
         RequiredIfOtherFieldsGiven.__init__(self, [other_field_name], error_message)
313 313
 
314 314
 class RequiredIfOtherFieldEquals(object):
315  
-    def __init__(self, other_field, other_value, error_message=None):
  315
+    def __init__(self, other_field, other_value, error_message=None, other_label=None):
316 316
         self.other_field = other_field
317 317
         self.other_value = other_value
  318
+        other_label = other_label or other_value
318 319
         self.error_message = error_message or lazy_inter(gettext_lazy("This field must be given if %(field)s is %(value)s"), {
319  
-            'field': other_field, 'value': other_value})
  320
+            'field': other_field, 'value': other_label})
320 321
         self.always_test = True
321 322
 
322 323
     def __call__(self, field_data, all_data):
@@ -324,11 +325,12 @@ def __call__(self, field_data, all_data):
324 325
             raise ValidationError(self.error_message)
325 326
 
326 327
 class RequiredIfOtherFieldDoesNotEqual(object):
327  
-    def __init__(self, other_field, other_value, error_message=None):
  328
+    def __init__(self, other_field, other_value, other_label=None, error_message=None):
328 329
         self.other_field = other_field
329 330
         self.other_value = other_value
  331
+        other_label = other_label or other_value
330 332
         self.error_message = error_message or lazy_inter(gettext_lazy("This field must be given if %(field)s is not %(value)s"), {
331  
-            'field': other_field, 'value': other_value})
  333
+            'field': other_field, 'value': other_label})
332 334
         self.always_test = True
333 335
 
334 336
     def __call__(self, field_data, all_data):
4  docs/forms.txt
@@ -608,6 +608,10 @@ fails. If no message is passed in, a default message is used.
608 608
     order). If the given field does (or does not have, in the latter case) the
609 609
     given value, then the current field being validated is required.
610 610
 
  611
+    An optional ``other_label`` argument can be passed which, if given, is used
  612
+    in error messages instead of the value. This allows more user friendly error
  613
+    messages if the value itself is not descriptive enough.
  614
+
611 615
     Note that because validators are called before any ``do_html2python()``
612 616
     functions, the value being compared against is a string. So
613 617
     ``RequiredIfOtherFieldEquals('choice', '1')`` is correct, whilst

0 notes on commit cef2239

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