Fixed #23151 -- Deprecated RegexField.error_message.

Thanks Baptiste Mispelon for the suggestion.
This commit is contained in:
Tim Graham 2014-08-02 18:33:18 -04:00
parent 44169a00c1
commit 0f2ceee025
6 changed files with 31 additions and 8 deletions

View File

@ -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 File

@ -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

View File

@ -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 File

@ -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 File

@ -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.

View File

@ -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')