kopia lustrzana https://github.com/wagtail/wagtail
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.pull/2794/head
rodzic
ef05d37686
commit
5758b54f2e
|
@ -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
|
||||
~~~~~~~~
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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)]))
|
||||
|
|
Ładowanie…
Reference in New Issue