From 0f2ceee0254349ee4ac7d472d3efe67ee161a917 Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Sat, 2 Aug 2014 18:33:18 -0400 Subject: [PATCH] Fixed #23151 -- Deprecated RegexField.error_message. Thanks Baptiste Mispelon for the suggestion. --- django/contrib/flatpages/forms.py | 7 +++++-- django/forms/fields.py | 7 ++++++- docs/internals/deprecation.txt | 2 ++ docs/ref/forms/fields.txt | 11 +++++++---- docs/releases/1.8.txt | 6 ++++++ tests/forms_tests/tests/test_fields.py | 6 +++++- 6 files changed, 31 insertions(+), 8 deletions(-) diff --git a/django/contrib/flatpages/forms.py b/django/contrib/flatpages/forms.py index 8d85922d9d..6e7448c397 100644 --- a/django/contrib/flatpages/forms.py +++ b/django/contrib/flatpages/forms.py @@ -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 diff --git a/django/forms/fields.py b/django/forms/fields.py index 6e44ccfbb7..36f13472eb 100644 --- a/django/forms/fields.py +++ b/django/forms/fields.py @@ -25,7 +25,7 @@ from django.forms.widgets import ( 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 @@ class RegexField(CharField): """ # 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 diff --git a/docs/internals/deprecation.txt b/docs/internals/deprecation.txt index f1cdfdb6be..795e46f8f0 100644 --- a/docs/internals/deprecation.txt +++ b/docs/internals/deprecation.txt @@ -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 diff --git a/docs/ref/forms/fields.txt b/docs/ref/forms/fields.txt index e92013a832..7dd0510237 100644 --- a/docs/ref/forms/fields.txt +++ b/docs/ref/forms/fields.txt @@ -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`` ~~~~~~~~~~~~~ diff --git a/docs/releases/1.8.txt b/docs/releases/1.8.txt index a03b37c505..07dd783d0b 100644 --- a/docs/releases/1.8.txt +++ b/docs/releases/1.8.txt @@ -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. diff --git a/tests/forms_tests/tests/test_fields.py b/tests/forms_tests/tests/test_fields.py index 0b82b3940b..b7786adf00 100644 --- a/tests/forms_tests/tests/test_fields.py +++ b/tests/forms_tests/tests/test_fields.py @@ -32,6 +32,7 @@ import re import os from decimal import Decimal from unittest import skipIf +import warnings try: from PIL import Image @@ -630,7 +631,10 @@ class FieldsTests(SimpleTestCase): 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')