kopia lustrzana https://github.com/wagtail/wagtail
Implement max_length validation on RichTextBlock
rodzic
8970568b93
commit
3fa5eb125c
|
@ -205,6 +205,7 @@ All block definitions accept the following optional keyword arguments:
|
|||
:param editor: The rich text editor to be used (see :ref:`WAGTAILADMIN_RICH_TEXT_EDITORS`).
|
||||
:param features: Specifies the set of features allowed (see :ref:`rich_text_features`).
|
||||
:param required: If true (the default), the field cannot be left blank.
|
||||
:param max_length: The maximum allowed length of the field. Only text is counted; rich text formatting and embedded content does not count towards the limit.
|
||||
:param help_text: Help text to display alongside the field.
|
||||
:param validators: A list of validation functions for the field (see `Django Validators <https://docs.djangoproject.com/en/stable/ref/validators/>`__).
|
||||
:param form_classname: A value to add to the form field's ``class`` attribute when rendered on the page editing form.
|
||||
|
|
|
@ -13,7 +13,11 @@ from django.utils.translation import gettext as _
|
|||
|
||||
from wagtail.admin.staticfiles import versioned_static
|
||||
from wagtail.coreutils import camelcase_to_underscore, resolve_model_string
|
||||
from wagtail.rich_text import RichText, get_text_for_indexing
|
||||
from wagtail.rich_text import (
|
||||
RichText,
|
||||
RichTextMaxLengthValidator,
|
||||
get_text_for_indexing,
|
||||
)
|
||||
from wagtail.telepath import Adapter, register
|
||||
|
||||
from .base import Block
|
||||
|
@ -642,9 +646,14 @@ class RichTextBlock(FieldBlock):
|
|||
help_text=None,
|
||||
editor="default",
|
||||
features=None,
|
||||
max_length=None,
|
||||
validators=(),
|
||||
**kwargs,
|
||||
):
|
||||
if max_length is not None:
|
||||
validators = list(validators) + [
|
||||
RichTextMaxLengthValidator(max_length),
|
||||
]
|
||||
self.field_options = {
|
||||
"required": required,
|
||||
"help_text": help_text,
|
||||
|
|
|
@ -629,6 +629,20 @@ class TestRichTextBlock(TestCase):
|
|||
with self.assertRaises(ValidationError):
|
||||
block.clean(RichText("<p>bar</p>"))
|
||||
|
||||
def test_validate_max_length(self):
|
||||
block = blocks.RichTextBlock(max_length=20)
|
||||
|
||||
block.clean(RichText("<p>short</p>"))
|
||||
|
||||
with self.assertRaises(ValidationError):
|
||||
block.clean(RichText("<p>this exceeds the 20 character limit</p>"))
|
||||
|
||||
block.clean(
|
||||
RichText(
|
||||
'<p><a href="http://really-long-domain-name.example.com">also</a> short</p>'
|
||||
)
|
||||
)
|
||||
|
||||
def test_get_searchable_content(self):
|
||||
block = blocks.RichTextBlock()
|
||||
value = RichText(
|
||||
|
|
Ładowanie…
Reference in New Issue