Permalink
Browse files

Fixed #23151 -- Deprecated RegexField.error_message.

Thanks Baptiste Mispelon for the suggestion.
  • Loading branch information...
timgraham committed Aug 2, 2014
1 parent 44169a0 commit 0f2ceee0254349ee4ac7d472d3efe67ee161a917
@@ -8,8 +8,11 @@ class FlatpageForm(forms.ModelForm):
url = forms.RegexField(label=_("URL"), max_length=100, regex=r'^[-\w/\.~]+$',
help_text=_("Example: '/about/contact/'. Make sure to have leading"
" and trailing slashes."),
- error_message=_("This value must contain only letters, numbers,"
- " dots, underscores, dashes, slashes or tildes."))
+ error_messages={
+ "invalid": _("This value must contain only letters, numbers,"
+ " dots, underscores, dashes, slashes or tildes."),
+ },
+ )
class Meta:
model = FlatPage
View
@@ -25,7 +25,7 @@
from django.utils import formats
from django.utils.encoding import smart_text, force_str, force_text
from django.utils.ipv6 import clean_ipv6_address
-from django.utils.deprecation import RemovedInDjango19Warning
+from django.utils.deprecation import RemovedInDjango19Warning, RemovedInDjango20Warning
from django.utils import six
from django.utils.six.moves.urllib.parse import urlsplit, urlunsplit
from django.utils.translation import ugettext_lazy as _, ungettext_lazy
@@ -531,6 +531,11 @@ def __init__(self, regex, max_length=None, min_length=None, error_message=None,
"""
# error_message is just kept for backwards compatibility:
if error_message is not None:
+ warnings.warn(
+ "The 'error_message' argument is deprecated. Use "
+ "Field.error_messages['invalid'] instead.",
+ RemovedInDjango20Warning, stacklevel=2
+ )
error_messages = kwargs.get('error_messages') or {}
error_messages['invalid'] = error_message
kwargs['error_messages'] = error_messages
@@ -40,6 +40,8 @@ about each item can often be found in the release notes of two versions prior.
* ``django.template.resolve_variable`` will be removed.
+* The ``error_message`` argument of ``django.forms.RegexField`` will be removed.
+
.. _deprecation-removed-in-1.9:
1.9
View
@@ -805,10 +805,13 @@ For each field, we describe the default widget used if you don't specify
Also takes ``max_length`` and ``min_length``, which work just as they do for
``CharField``.
- The optional argument ``error_message`` is also accepted for backwards
- compatibility. The preferred way to provide an error message is to use the
- ``error_messages`` argument, passing a dictionary with ``'invalid'`` as a key
- and the error message as the value.
+ .. deprecated:: 1.8
+
+ The optional argument ``error_message`` is also accepted for backwards
+ compatibility but will be removed in Django 2.0. The preferred way to
+ provide an error message is to use the :attr:`~Field.error_messages`
+ argument, passing a dictionary with ``'invalid'`` as a key and the error
+ message as the value.
``SlugField``
~~~~~~~~~~~~~
View
@@ -625,3 +625,9 @@ The function has been informally marked as "Deprecated" for some time. Replace
It provided the :ttag:`lorem` template tag which is now included in the
built-in tags. Simply remove ``'django.contrib.webdesign'`` from
:setting:`INSTALLED_APPS` and ``{% load webdesign %}`` from your templates.
+
+``error_message`` argument to ``django.forms.RegexField``
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+It provided backwards compatibility for pre-1.0 code, but its functionality is
+redundant. Use ``Field.error_messages['invalid']`` instead.
@@ -32,6 +32,7 @@
import os
from decimal import Decimal
from unittest import skipIf
+import warnings
try:
from PIL import Image
@@ -630,7 +631,10 @@ def test_regexfield_3(self):
self.assertRaisesMessage(ValidationError, "'Enter a valid value.'", f.clean, '2A2 ')
def test_regexfield_4(self):
- f = RegexField('^[0-9][0-9][0-9][0-9]$', error_message='Enter a four-digit number.')
+ # deprecated error_message argument; remove in Django 2.0
+ with warnings.catch_warnings(record=True) as w:
+ warnings.simplefilter("always")
+ f = RegexField('^[0-9][0-9][0-9][0-9]$', error_message='Enter a four-digit number.')
self.assertEqual('1234', f.clean('1234'))
self.assertRaisesMessage(ValidationError, "'Enter a four-digit number.'", f.clean, '123')
self.assertRaisesMessage(ValidationError, "'Enter a four-digit number.'", f.clean, 'abcd')

0 comments on commit 0f2ceee

Please sign in to comment.