From 5758b54f2eac32640d408de1228bc04a8c0d5bde Mon Sep 17 00:00:00 2001 From: Matt Westcott Date: Tue, 28 Jun 2016 15:29:59 +0100 Subject: [PATCH] Support `error_messages` dict as argument to RegexBlock The individual `error_message` kwarg on RegexField is deprecated in Django 1.8 (and removed in Django 1.10), so it's appropriate for RegexBlock to follow the same convention. --- docs/topics/streamfield.rst | 10 ++++++++- wagtail/wagtailcore/blocks/field_block.py | 4 ++-- wagtail/wagtailcore/tests/test_blocks.py | 27 +++++++++++++++++++---- 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/docs/topics/streamfield.rst b/docs/topics/streamfield.rst index 5e747dde49..f056a2d513 100644 --- a/docs/topics/streamfield.rst +++ b/docs/topics/streamfield.rst @@ -122,7 +122,15 @@ RegexBlock ``wagtail.wagtailcore.blocks.RegexBlock`` -A single-line text input that validates a string against a regex expression. The regular expression used for validation must be supplied as the first argument, or as the keyword argument ``regex``. The message text used to indicate a validation error can be customised using the ``error_message`` keyword argument to pass a custom message. The keyword arguments ``regex``, ``required``, ``max_length``, ``min_length`` and ``error_message`` are accepted. +A single-line text input that validates a string against a regex expression. The regular expression used for validation must be supplied as the first argument, or as the keyword argument ``regex``. To customise the message text used to indicate a validation error, pass a dictionary as the keyword argument ``error_messages`` containing either or both of the keys ``required`` (for the message shown on an empty value) or ``invalid`` (for the message shown on a non-matching value): + +.. code-block:: python + + blocks.RegexBlock(regex=r'^[0-9]{3}$', error_message={ + 'invalid': "Not a valid library card number." + }) + +The keyword arguments ``regex``, ``required``, ``max_length``, ``min_length`` and ``error_messages`` are accepted. URLBlock ~~~~~~~~ diff --git a/wagtail/wagtailcore/blocks/field_block.py b/wagtail/wagtailcore/blocks/field_block.py index cf3d378afb..b2e616b3cc 100644 --- a/wagtail/wagtailcore/blocks/field_block.py +++ b/wagtail/wagtailcore/blocks/field_block.py @@ -164,13 +164,13 @@ class DecimalBlock(FieldBlock): class RegexBlock(FieldBlock): def __init__(self, regex, required=True, max_length=None, min_length=None, - error_message=None, *args, **kwargs): + error_messages=None, *args, **kwargs): self.field = forms.RegexField( regex=regex, required=required, max_length=max_length, min_length=min_length, - error_message=error_message, + error_messages=error_messages, ) super(RegexBlock, self).__init__(*args, **kwargs) diff --git a/wagtail/wagtailcore/tests/test_blocks.py b/wagtail/wagtailcore/tests/test_blocks.py index 34167ad345..b8743af197 100644 --- a/wagtail/wagtailcore/tests/test_blocks.py +++ b/wagtail/wagtailcore/tests/test_blocks.py @@ -274,22 +274,41 @@ class TestRegexBlock(TestCase): def test_raises_required_error(self): block = blocks.RegexBlock(regex=r'^[0-9]{3}$') - with self.assertRaises(ValidationError): + with self.assertRaises(ValidationError) as context: block.clean("") + self.assertIn('This field is required.', context.exception.messages) + + def test_raises_custom_required_error(self): + test_message = 'Oops, you missed a bit.' + block = blocks.RegexBlock(regex=r'^[0-9]{3}$', error_messages={ + 'required': test_message, + }) + + with self.assertRaises(ValidationError) as context: + block.clean("") + + self.assertIn(test_message, context.exception.messages) + def test_raises_validation_error(self): block = blocks.RegexBlock(regex=r'^[0-9]{3}$') - with self.assertRaises(ValidationError): + with self.assertRaises(ValidationError) as context: block.clean("[/]") + self.assertIn('Enter a valid value.', context.exception.messages) + def test_raises_custom_error_message(self): test_message = 'Not a valid library card number.' - block = blocks.RegexBlock(regex=r'^[0-9]{3}$', error_message=test_message) + block = blocks.RegexBlock(regex=r'^[0-9]{3}$', error_messages={ + 'invalid': test_message + }) - with self.assertRaises(ValidationError): + with self.assertRaises(ValidationError) as context: block.clean("[/]") + self.assertIn(test_message, context.exception.messages) + html = block.render_form( "[/]", errors=ErrorList([ValidationError(test_message)]))