I know that errors can be added to the general admin form by using a custom form class (subclassing WagtailAdminModelForm) and using self.add_error("<field_name>", "<error_message>"). However is it also possible to add error message to the inline form items of an inline panel?
For example in my case it is forbidden to add duplicate entries in the inline panel. When the user adds duplicate items and tries to submit, all duplicate items should be highlighted with an error message.
Edit: I need to do this for a Snippet (not a Page)
You can set a unique together constraint on the Orderable class that checks the ParentalKey field against the field that needs to be unique. Wagtail display an error above the orderable list and an error on each of the subsequent duplicate fields (but not the first).
For example, I have a
TemplateTextSetItemOrderable class with a ParentalKey fieldsetthat points to a ClusterableModel class:If I enter a duplicate value for
template_tagwithin the same parentTemplateTextinstance, I would get the following result:(1, 2 & 3 all have the same
template_tagvalue, 2 & 3 have field errors displayed on the form)If I was to enter 'test1' in a different parent instance, no error would be raised since it is unique in that instance.
unique_togetheractually has a deprecation warning in Django and should be written as:Currently, Wagtail doesn't handle errors raised by
UniqueConstraintand throws a 500 error instead. I think it might be coming in Wagtail 6.