Custom page validation

395 views
Skip to first unread message

Karen Tracey

unread,
Jun 4, 2015, 1:33:40 PM6/4/15
to wag...@googlegroups.com
Running 1.0 beta 2, I can add some custom page validation to my pages by simply defining a clean() method for my page and raising a ValidationError when I detect a condition where I don't want the page to be saved. I get a big white-on-red banner message at the top of the page saying "The page could not be created due to validation errors" but my own error message supplied by my ValidationError is not getting displayed anywhere I can see, so the admin user will be confused/frustrated as to what might be wrong with what they are trying to save.

Is there some reason form.non_field_errors is not getting displayed when pages are created/saved? Am I going down the wrong path of trying to provide custom validation by defining a clean() method for my page?

For what its worth if I add:

                {% if form.non_field_errors %}
                    <ul>
                        {% for error in form.non_field_errors %}
                            <li class="error">{{ error|escape }}</li>
                        {% endfor %}
                    </ul>
                {% endif %}

underneath the existing messages ul in wagtailadmin/base.html I then see my custom clean() error message underneath the generic "page could not be saved" method....

Matthew Westcott

unread,
Jun 5, 2015, 6:58:26 AM6/5/15
to wag...@googlegroups.com
Hi Karen,
I don't think there's any particular reason for non_field_errors to be left out of the form, except that we haven't used that mechanism ourselves and thus haven't got round to sorting out the styling issues: https://github.com/torchbox/wagtail/issues/536. Patches welcome!

Cheers,
- Matt
Reply all
Reply to author
Forward
0 new messages