Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #17051 -- Removed some 'invalid' field error messages

When the 'invalid' error message is set at field level, it masks
the error message raised by the validator, if any.
  • Loading branch information...
commit 2f121dfe635b3f497fe1fe03bc8eb97cdf5083b3 1 parent 34d0986
Claude Paroz authored March 14, 2013
5  django/contrib/auth/tests/forms.py
@@ -7,7 +7,7 @@
7 7
     ReadOnlyPasswordHashField, ReadOnlyPasswordHashWidget)
8 8
 from django.contrib.auth.tests.utils import skipIfCustomUser
9 9
 from django.core import mail
10  
-from django.forms.fields import Field, EmailField, CharField
  10
+from django.forms.fields import Field, CharField
11 11
 from django.test import TestCase
12 12
 from django.test.utils import override_settings
13 13
 from django.utils.encoding import force_text
@@ -322,8 +322,7 @@ def test_invalid_email(self):
322 322
         data = {'email': 'not valid'}
323 323
         form = PasswordResetForm(data)
324 324
         self.assertFalse(form.is_valid())
325  
-        self.assertEqual(form['email'].errors,
326  
-                         [force_text(EmailField.default_error_messages['invalid'])])
  325
+        self.assertEqual(form['email'].errors, [_('Enter a valid email address.')])
327 326
 
328 327
     def test_nonexistant_email(self):
329 328
         # Test nonexistant email address. This should not fail because it would
2  django/core/validators.py
@@ -80,7 +80,7 @@ def validate_integer(value):
80 80
 
81 81
 
82 82
 class EmailValidator(object):
83  
-    message = _('Enter a valid e-mail address.')
  83
+    message = _('Enter a valid email address.')
84 84
     code = 'invalid'
85 85
     user_regex = re.compile(
86 86
         r"(^[-!#$%&'*+/=?^_`{}|~0-9A-Z]+(\.[-!#$%&'*+/=?^_`{}|~0-9A-Z]+)*$"  # dot-atom
24  django/forms/fields.py
@@ -49,9 +49,10 @@ class Field(object):
49 49
     widget = TextInput # Default widget to use when rendering this type of Field.
50 50
     hidden_widget = HiddenInput # Default widget to use when rendering this as "hidden".
51 51
     default_validators = [] # Default set of validators
  52
+    # Add an 'invalid' entry to default_error_message if you want a specific
  53
+    # field error message not raised by the field validators.
52 54
     default_error_messages = {
53 55
         'required': _('This field is required.'),
54  
-        'invalid': _('Enter a valid value.'),
55 56
     }
56 57
     empty_values = list(validators.EMPTY_VALUES)
57 58
 
@@ -225,8 +226,6 @@ def widget_attrs(self, widget):
225 226
 class IntegerField(Field):
226 227
     default_error_messages = {
227 228
         'invalid': _('Enter a whole number.'),
228  
-        'max_value': _('Ensure this value is less than or equal to %(limit_value)s.'),
229  
-        'min_value': _('Ensure this value is greater than or equal to %(limit_value)s.'),
230 229
     }
231 230
 
232 231
     def __init__(self, max_value=None, min_value=None, *args, **kwargs):
@@ -504,9 +503,6 @@ def _set_regex(self, regex):
504 503
 
505 504
 class EmailField(CharField):
506 505
     widget = EmailInput
507  
-    default_error_messages = {
508  
-        'invalid': _('Enter a valid email address.'),
509  
-    }
510 506
     default_validators = [validators.validate_email]
511 507
 
512 508
     def clean(self, value):
@@ -1082,9 +1078,6 @@ def compress(self, data_list):
1082 1078
 
1083 1079
 
1084 1080
 class IPAddressField(CharField):
1085  
-    default_error_messages = {
1086  
-        'invalid': _('Enter a valid IPv4 address.'),
1087  
-    }
1088 1081
     default_validators = [validators.validate_ipv4_address]
1089 1082
 
1090 1083
     def to_python(self, value):
@@ -1094,13 +1087,9 @@ def to_python(self, value):
1094 1087
 
1095 1088
 
1096 1089
 class GenericIPAddressField(CharField):
1097  
-    default_error_messages = {}
1098  
-
1099 1090
     def __init__(self, protocol='both', unpack_ipv4=False, *args, **kwargs):
1100 1091
         self.unpack_ipv4 = unpack_ipv4
1101  
-        self.default_validators, invalid_error_message = \
1102  
-            validators.ip_address_validators(protocol, unpack_ipv4)
1103  
-        self.default_error_messages['invalid'] = invalid_error_message
  1092
+        self.default_validators = validators.ip_address_validators(protocol, unpack_ipv4)[0]
1104 1093
         super(GenericIPAddressField, self).__init__(*args, **kwargs)
1105 1094
 
1106 1095
     def to_python(self, value):
@@ -1108,14 +1097,9 @@ def to_python(self, value):
1108 1097
             return ''
1109 1098
         value = value.strip()
1110 1099
         if value and ':' in value:
1111  
-            return clean_ipv6_address(value,
1112  
-                self.unpack_ipv4, self.error_messages['invalid'])
  1100
+            return clean_ipv6_address(value, self.unpack_ipv4)
1113 1101
         return value
1114 1102
 
1115 1103
 
1116 1104
 class SlugField(CharField):
1117  
-    default_error_messages = {
1118  
-        'invalid': _("Enter a valid 'slug' consisting of letters, numbers,"
1119  
-                     " underscores or hyphens."),
1120  
-    }
1121 1105
     default_validators = [validators.validate_slug]
2  django/utils/ipv6.py
@@ -5,7 +5,7 @@
5 5
 from django.utils.six.moves import xrange
6 6
 
7 7
 def clean_ipv6_address(ip_str, unpack_ipv4=False,
8  
-        error_message="This is not a valid IPv6 address"):
  8
+        error_message="This is not a valid IPv6 address."):
9 9
     """
10 10
     Cleans a IPv6 address string.
11 11
 
20  docs/ref/forms/validation.txt
@@ -181,24 +181,20 @@ the field's ``validators`` argument, or defined on the Field class itself with
181 181
 the ``default_validators`` attribute.
182 182
 
183 183
 Simple validators can be used to validate values inside the field, let's have
184  
-a look at Django's ``EmailField``::
  184
+a look at Django's ``SlugField``::
185 185
 
186  
-    class EmailField(CharField):
187  
-        default_error_messages = {
188  
-            'invalid': _('Enter a valid email address.'),
189  
-        }
190  
-        default_validators = [validators.validate_email]
  186
+    class SlugField(CharField):
  187
+        default_validators = [validators.validate_slug]
191 188
 
192  
-As you can see, ``EmailField`` is just a ``CharField`` with customized error
193  
-message and a validator that validates email addresses. This can also be done
194  
-on field definition so::
  189
+As you can see, ``SlugField`` is just a ``CharField`` with a customized
  190
+validator that validates that submitted text obeys to some character rules.
  191
+This can also be done on field definition so::
195 192
 
196  
-    email = forms.EmailField()
  193
+    slug = forms.SlugField()
197 194
 
198 195
 is equivalent to::
199 196
 
200  
-    email = forms.CharField(validators=[validators.validate_email],
201  
-            error_messages={'invalid': _('Enter a valid email address.')})
  197
+    slug = forms.CharField(validators=[validators.validate_slug])
202 198
 
203 199
 
204 200
 Form field default cleaning
30  tests/forms_tests/tests/extra.py
@@ -506,11 +506,11 @@ def test_generic_ipaddress_as_generic(self):
506 506
         self.assertFormErrors(['Enter a valid IPv4 or IPv6 address.'], f.clean, '256.125.1.5')
507 507
         self.assertEqual(f.clean(' fe80::223:6cff:fe8a:2e8a '), 'fe80::223:6cff:fe8a:2e8a')
508 508
         self.assertEqual(f.clean(' 2a02::223:6cff:fe8a:2e8a '), '2a02::223:6cff:fe8a:2e8a')
509  
-        self.assertFormErrors(['Enter a valid IPv4 or IPv6 address.'], f.clean, '12345:2:3:4')
510  
-        self.assertFormErrors(['Enter a valid IPv4 or IPv6 address.'], f.clean, '1::2:3::4')
511  
-        self.assertFormErrors(['Enter a valid IPv4 or IPv6 address.'], f.clean, 'foo::223:6cff:fe8a:2e8a')
512  
-        self.assertFormErrors(['Enter a valid IPv4 or IPv6 address.'], f.clean, '1::2:3:4:5:6:7:8')
513  
-        self.assertFormErrors(['Enter a valid IPv4 or IPv6 address.'], f.clean, '1:2')
  509
+        self.assertFormErrors(['This is not a valid IPv6 address.'], f.clean, '12345:2:3:4')
  510
+        self.assertFormErrors(['This is not a valid IPv6 address.'], f.clean, '1::2:3::4')
  511
+        self.assertFormErrors(['This is not a valid IPv6 address.'], f.clean, 'foo::223:6cff:fe8a:2e8a')
  512
+        self.assertFormErrors(['This is not a valid IPv6 address.'], f.clean, '1::2:3:4:5:6:7:8')
  513
+        self.assertFormErrors(['This is not a valid IPv6 address.'], f.clean, '1:2')
514 514
 
515 515
     def test_generic_ipaddress_as_ipv4_only(self):
516 516
         f = GenericIPAddressField(protocol="IPv4")
@@ -535,11 +535,11 @@ def test_generic_ipaddress_as_ipv6_only(self):
535 535
         self.assertFormErrors(['Enter a valid IPv6 address.'], f.clean, '256.125.1.5')
536 536
         self.assertEqual(f.clean(' fe80::223:6cff:fe8a:2e8a '), 'fe80::223:6cff:fe8a:2e8a')
537 537
         self.assertEqual(f.clean(' 2a02::223:6cff:fe8a:2e8a '), '2a02::223:6cff:fe8a:2e8a')
538  
-        self.assertFormErrors(['Enter a valid IPv6 address.'], f.clean, '12345:2:3:4')
539  
-        self.assertFormErrors(['Enter a valid IPv6 address.'], f.clean, '1::2:3::4')
540  
-        self.assertFormErrors(['Enter a valid IPv6 address.'], f.clean, 'foo::223:6cff:fe8a:2e8a')
541  
-        self.assertFormErrors(['Enter a valid IPv6 address.'], f.clean, '1::2:3:4:5:6:7:8')
542  
-        self.assertFormErrors(['Enter a valid IPv6 address.'], f.clean, '1:2')
  538
+        self.assertFormErrors(['This is not a valid IPv6 address.'], f.clean, '12345:2:3:4')
  539
+        self.assertFormErrors(['This is not a valid IPv6 address.'], f.clean, '1::2:3::4')
  540
+        self.assertFormErrors(['This is not a valid IPv6 address.'], f.clean, 'foo::223:6cff:fe8a:2e8a')
  541
+        self.assertFormErrors(['This is not a valid IPv6 address.'], f.clean, '1::2:3:4:5:6:7:8')
  542
+        self.assertFormErrors(['This is not a valid IPv6 address.'], f.clean, '1:2')
543 543
 
544 544
     def test_generic_ipaddress_as_generic_not_required(self):
545 545
         f = GenericIPAddressField(required=False)
@@ -552,11 +552,11 @@ def test_generic_ipaddress_as_generic_not_required(self):
552 552
         self.assertFormErrors(['Enter a valid IPv4 or IPv6 address.'], f.clean, '256.125.1.5')
553 553
         self.assertEqual(f.clean(' fe80::223:6cff:fe8a:2e8a '), 'fe80::223:6cff:fe8a:2e8a')
554 554
         self.assertEqual(f.clean(' 2a02::223:6cff:fe8a:2e8a '), '2a02::223:6cff:fe8a:2e8a')
555  
-        self.assertFormErrors(['Enter a valid IPv4 or IPv6 address.'], f.clean, '12345:2:3:4')
556  
-        self.assertFormErrors(['Enter a valid IPv4 or IPv6 address.'], f.clean, '1::2:3::4')
557  
-        self.assertFormErrors(['Enter a valid IPv4 or IPv6 address.'], f.clean, 'foo::223:6cff:fe8a:2e8a')
558  
-        self.assertFormErrors(['Enter a valid IPv4 or IPv6 address.'], f.clean, '1::2:3:4:5:6:7:8')
559  
-        self.assertFormErrors(['Enter a valid IPv4 or IPv6 address.'], f.clean, '1:2')
  555
+        self.assertFormErrors(['This is not a valid IPv6 address.'], f.clean, '12345:2:3:4')
  556
+        self.assertFormErrors(['This is not a valid IPv6 address.'], f.clean, '1::2:3::4')
  557
+        self.assertFormErrors(['This is not a valid IPv6 address.'], f.clean, 'foo::223:6cff:fe8a:2e8a')
  558
+        self.assertFormErrors(['This is not a valid IPv6 address.'], f.clean, '1::2:3:4:5:6:7:8')
  559
+        self.assertFormErrors(['This is not a valid IPv6 address.'], f.clean, '1:2')
560 560
 
561 561
     def test_generic_ipaddress_normalization(self):
562 562
         # Test the normalising code
33  tests/forms_tests/tests/validators.py
... ...
@@ -1,16 +1,39 @@
  1
+from __future__ import unicode_literals
  2
+
1 3
 from django import forms
2 4
 from django.core import validators
3 5
 from django.core.exceptions import ValidationError
4 6
 from django.utils.unittest import TestCase
5 7
 
6 8
 
  9
+class UserForm(forms.Form):
  10
+    full_name = forms.CharField(
  11
+        max_length = 50,
  12
+        validators = [
  13
+            validators.validate_integer,
  14
+            validators.validate_email,
  15
+        ]
  16
+    )
  17
+    string = forms.CharField(
  18
+        max_length = 50,
  19
+        validators = [
  20
+            validators.RegexValidator(
  21
+                regex='^[a-zA-Z]*$',
  22
+                message="Letters only.",
  23
+            )
  24
+        ]
  25
+    )
  26
+
  27
+
7 28
 class TestFieldWithValidators(TestCase):
8 29
     def test_all_errors_get_reported(self):
9  
-        field = forms.CharField(
10  
-            validators=[validators.validate_integer, validators.validate_email]
11  
-        )
12  
-        self.assertRaises(ValidationError, field.clean, 'not int nor mail')
  30
+        form = UserForm({'full_name': 'not int nor mail', 'string': '2 is not correct'})
  31
+        self.assertRaises(ValidationError, form.fields['full_name'].clean, 'not int nor mail')
  32
+
13 33
         try:
14  
-            field.clean('not int nor mail')
  34
+            form.fields['full_name'].clean('not int nor mail')
15 35
         except ValidationError as e:
16 36
             self.assertEqual(2, len(e.messages))
  37
+
  38
+        self.assertFalse(form.is_valid())
  39
+        self.assertEqual(form.errors['string'], ["Letters only."])

0 notes on commit 2f121df

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