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.

Wagtail Validation for Blocks

See original GitHub issue

There is no documentation on how the validation for blocks works.

Pertinent section of the Wagtail docs

https://docs.wagtail.io/en/v2.13/advanced_topics/customisation/streamfield_blocks.html

Details

I just updated a project to Wagtail 2.13. Having some tests in place which check whether some custom validation logic within the clean method of our blocks works we noticed these tests failing.

The tests check whether the error messages actually appear within the wagtail editing interface.

Please consider following example:

    def clean(self, value):
        if bool(value.get("link_text")) != bool(value.get("url")):
            raise ValidationError(
                "ValidationError in DllElementBlock",
                params={
                    "link_text": ErrorList(["Please fill both fields."])
                },
            )
        return super().clean(value)

I have a StructBlock with a field url and a field link_text. Both fields need to be either empty or filled.

After the upgrade the error message did not appear.

I changed the validation to the following:

    def clean(self, value):
        if bool(value.get("link_text")) != bool(value.get("url")):
            raise StructBlockValidationError(   # <--- changed exception
                {"link_text": ErrorList(["Please fill both fields."])},
            )
        return super().clean(value)

Using StructBlockValidationError fixed the issue! The error message is shown.

I would suggest to add some documentation on how to validate blocks and which exceptions to use.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:7
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
gasmancommented, Sep 20, 2021

The current implementation of validation for StructBlock / ListBlock / StreamBlock was not really intended to be used for custom validation in end-user code - it was only designed to deal with propagating errors that occur in FieldBlocks, and there are some design decisions that made sense in that context but are more dubious when the error originates from the StructBlock / ListBlock / StreamBlock itself - e.g. the data of a ListBlock validation error being a list of either None or a ValidationError instance for each of its child blocks. If we’re going to make this a developer-facing API (and I think we should…) then we really need to redesign it to be more user-friendly, rather than document what’s already there.

1reaction
nutjob4lifecommented, Dec 8, 2021

Just for context, @KalobTaulien includes a lesson on StreamField validation affected by this 😌

Read more comments on GitHub >

github_iconTop Results From Across the Web

StreamField block reference - Wagtail's documentation
This document details the block types provided by Wagtail for use in StreamField, and how they can be combined into new block types....
Read more >
Custom StreamField Field Validation - LearnWagtail.com
In this tutorial, let's cover just one of these situations and we want the user to enter just one field, but both fields...
Read more >
Custom StreamField blocks and field validation - Google Groups
Has anybody done this or can point me in the right direction? From looking at the wagtail source no clean_foo methods are called...
Read more >
Freeform page content using StreamField — Wagtail 2.2.2 ...
'name' is used to identify the block type within templates and the internal JSON representation (and should follow standard Python conventions for variable ......
Read more >
validate no duplicates in streamfield - django - Stack Overflow
Also, the value presented to the validator is an instance of [StreamValue](https://github.com/wagtail/wagtail/blob/master/wagtail/core/blocks/ ...
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