question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

blank=False validation on RichTextField / RichTextBlock is applied inconsistently

See original GitHub issue

Issue 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:closed
  • Created 5 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
harrislapiroffcommented, Jun 10, 2019

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

{% load wagtailcore_tags %}

{% if model.text_field %}
    <div class="warning">
        {{ model.text_field|richtext }}
    </div>
{% endif %}

For those sites I’ve written this simple filter as a workaround:

from django import template

register = template.Library()

@register.filter
def richtext_isempty(value):
    """
    Returns True if a value is an empty string, none, or a string containing
    nothing but paragraph tags
    """

    return any([
        value is None,
        value == '',
        value == '<p></p>',
    ])

Which can be used like:

{% load wagtailcore_tags my_tags %}

{% if not model.text_field|richtext_isempty %}
    <div class="warning">
        {{ model.text_field|richtext }}
    </div>
{% endif %}
0reactions
gasmancommented, Jun 4, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Rich text field in Salesforce doesnt works with ISBLANK and ...
I have a rich text field on my object and have validation to check if it is blank I want to throw an...
Read more >
Is it possible to create an Input Validation of a Rich Text Field?
This allows you to use its Input Validation formula to point to the Rich Text field. If needed the Text field could be...
Read more >
RichTextField vs Streamfields RichTextBlock styling first letter ...
Now I am having a bit of an annoyance with the two. I need to specially style the first letter of the paragraphs....
Read more >
RichTextBox Class (System.Windows.Forms) | Microsoft Learn
The RichTextBox is typically used to provide text manipulation and display ... Gets or sets a value indicating whether the control causes validation...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found