Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wagtail ModelAdmin cleaning and validating fields that depend on each other

In Django you can add a clean method to a form to validate fields that depend on each other:

def clean_recipients(self):
    data = self.cleaned_data['recipients']
    if "[email protected]" not in data:
        raise ValidationError("You have forgotten about Fred!")

    # Always return a value to use as the new cleaned data, even if
    # this method didn't change it.
    return data

How can I add a custom form with a clean method to Wagtail ModelAdmin?

Wagtail has the panels concept and dynamically builds the form. I can't find anything about overriding the form. There is information about customising the create and update views. Custom views seem a bit cumbersome.

like image 260
allcaps Avatar asked Sep 11 '25 01:09

allcaps


1 Answers

I got an answer via the Wagtail Slack form @CynthiaKiser.

The base_form_class lets you set a WagtailAdminModelForm on the model.

from wagtail.admin.forms import WagtailAdminModelForm

class Foo(models.Model):
    ...
    base_form_class = FooForm


class FooForm(WagtailAdminModelForm):  # For pages use WagtailAdminPageForm
    def clean(self):
        data = self.cleaned_data['recipients']
        if "[email protected]" not in data:
            raise ValidationError("You have forgotten about Fred!")
        return data

All of the CRUD forms you encounter in Wagtail are just Django ModelForm instances underneath, so these Django docs are relevant (thanks @ababic)

An alternative to base_form_class is to specify a clean method on the model. It will be called by any form.

from django.core.exceptions import ValidationError

class Foo(models.Model):
    ...
    def clean(self):
        if "[email protected]" not in self.recipients:
            raise ValidationError(
                {'recipients': _("You have forgotten about Fred!")}
            )
like image 97
allcaps Avatar answered Sep 13 '25 05:09

allcaps