blank=False validation on RichTextField / RichTextBlock is applied inconsistently
See original GitHub issueIssue Summary
Submitting an empty RichTextField
that has been defined as blank=False
only produces a validation error if the user focuses on the field before submitting the form.
This happens because the spacer paragraph insertion logic in wagtail.admin.rich_text.converters.html_to_contentstate
normalises empty rich text content to a single empty paragraph, whereas Draftail explicitly tests for this case and returns null
. If the user never focuses on the field, Draftail is never activated, and so the final value of the field (following conversion back to dbHTML) ends up as <p></p>
, which is considered non-empty by Django validation.
We propose that html_to_contentstate
should catch empty content as a special case, and return null
. (Doing this within html_to_contentstate
, rather than checking the initial string value before html->contentstate conversion, ensures that we also catch HTML constructions that are semantically empty but not equal to the empty string, such as <p> </p>
.)
Steps to Reproduce
From a fresh wagtail start
project:
./manage.py migrate
./manage.py createsuperuser
Update home/models.py:
from django.db import models
from wagtail.admin.edit_handlers import FieldPanel
from wagtail.core.fields import RichTextField
from wagtail.core.models import Page
class HomePage(Page):
body = RichTextField(null=False, blank=False)
content_panels = Page.content_panels + [
FieldPanel('body'),
]
Run:
./manage.py makemigrations # (set '' as the default value for the field)
./manage.py migrate
./manage.py runserver
Edit the homepage; click ‘save draft’ without focusing on the body field - no validation error
Edit the homepage; focus on the body field then click ‘save draft’ - validation error
Technical details
- Python version: 3.6.1
- Django version: 2.0.5
- Wagtail version: 2.0.1
- Browser version: Chrome/66.0.3359.117 on OS X 10.12.6
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (5 by maintainers)
Top GitHub Comments
Sharing my workaround for use in templates, in case it’s helpful to anyone else. There are some pages in our sites that rely on a check like
For those sites I’ve written this simple filter as a workaround:
Which can be used like:
Thanks @cspollar! Yep, looks like this has indeed been fixed - not sure exactly when, but it looks like the relevant bit of activation in Draftail (i.e. the bit that normalises a single empty paragraph to
null
) now happens on page load rather than on focus.