Code

Ticket #2266: given_validators.diff

File given_validators.diff, 3.3 KB (added by alang@…, 8 years ago)

Updated patches

Line 
1Index: django/core/validators.py
2===================================================================
3--- django/core/validators.py   (revision 3439)
4+++ django/core/validators.py   (working copy)
5@@ -258,23 +258,32 @@
6             for v in self.validator_list:
7                 v(field_data, all_data)
8 
9-class RequiredIfOtherFieldNotGiven(object):
10-    def __init__(self, other_field_name, error_message=gettext_lazy("Please enter something for at least one field.")):
11-        self.other, self.error_message = other_field_name, error_message
12+class RequiredIfOtherFieldsNotGiven(object):
13+    def __init__(self, other_field_names, error_message=gettext_lazy("Please enter something for at least one field.")):
14+        self.other, self.error_message = other_field_names, error_message
15         self.always_test = True
16 
17     def __call__(self, field_data, all_data):
18-        if not all_data.get(self.other, False) and not field_data:
19+        for field in self.other:
20+            if all_data.get(field, False):
21+                return
22+        if not field_data:
23             raise ValidationError, self.error_message
24 
25+class RequiredIfOtherFieldNotGiven(RequiredIfOtherFieldsNotGiven):
26+    def __init__(self, other_field_name, error_message=gettext_lazy("Please enter something for at least one field.")):
27+        RequiredIfOtherFieldsNotGiven.__init__(self, [other_field_name], error_message)
28+
29 class RequiredIfOtherFieldsGiven(object):
30     def __init__(self, other_field_names, error_message=gettext_lazy("Please enter both fields or leave them both empty.")):
31         self.other, self.error_message = other_field_names, error_message
32         self.always_test = True
33 
34     def __call__(self, field_data, all_data):
35+        if field_data:
36+            return
37         for field in self.other:
38-            if all_data.get(field, False) and not field_data:
39+            if all_data.get(field, False):
40                 raise ValidationError, self.error_message
41 
42 class RequiredIfOtherFieldGiven(RequiredIfOtherFieldsGiven):
43Index: docs/forms.txt
44===================================================================
45--- docs/forms.txt      (revision 3439)
46+++ docs/forms.txt      (working copy)
47@@ -519,14 +519,23 @@
48     ``other_vaue``, then the validators in ``validator_list`` are all run
49     against the current field.
50 
51+``RequiredIfOtherFieldGiven``
52+    Takes the name of the other field and this field is only required if the
53+    other field has a value.
54+
55+``RequiredIfOtherFieldsGiven``
56+    Similar to ``RequiredIfOtherFieldGiven``, except that it takes a list
57+    of field names and if any one of the supplied fields has a value
58+    provided, the field being validated is required.
59+
60 ``RequiredIfOtherFieldNotGiven``
61     Takes the name of the other field and this field is only required if the
62     other field has no value.
63 
64 ``RequiredIfOtherFieldsNotGiven``
65     Similar to ``RequiredIfOtherFieldNotGiven``, except that it takes a list
66-    of field names and if any one of the supplied fields does not have a value
67-    provided, the field being validated is required.
68+    of field names and if none of the supplied fields has a value provided,
69+    the field being validated is required.
70 
71 ``RequiredIfOtherFieldEquals`` and ``RequiredIfOtherFieldDoesNotEqual``
72     Each of these validator classes takes a field name and a value (in that