Recognise required_on_save flag being set on model fields

It is clear from the test models and bakerydemo that title is frequently declared with a plain FieldPanel rather than TitleFieldPanel, so we should not rely on the presence of TitleFieldPanel or an explicit required_on_save flag on the FieldPanel to enforce form-level validation on the title.
pull/12964/head
Matt Westcott 2025-02-28 00:12:31 +00:00 zatwierdzone przez Matt Westcott
rodzic abb7a687e4
commit 96f9ebe3f6
3 zmienionych plików z 17 dodań i 3 usunięć

Wyświetl plik

@ -25,7 +25,7 @@ class FieldPanel(Panel):
disable_comments=None,
permission=None,
read_only=False,
required_on_save=False,
required_on_save=None,
**kwargs,
):
super().__init__(**kwargs)
@ -55,7 +55,21 @@ class FieldPanel(Panel):
opts = {
"fields": [self.field_name],
}
if not self.required_on_save:
required_on_save = self.required_on_save
if required_on_save is None:
# If required_on_save is not explicitly set, treat it as false unless:
# - it corresponds to a model field with required_on_save=True (such as page title)
# - it corresponds to a non-null, non-text-typed model field (in which case a blank value
# is not valid at the database level)
try:
db_field = self.db_field
except FieldDoesNotExist:
required_on_save = False
else:
required_on_save = getattr(db_field, "required_on_save", False)
if not required_on_save:
opts["defer_required_on_fields"] = [self.field_name]
if self.widget:

Wyświetl plik

@ -32,7 +32,6 @@ class TitleFieldPanel(FieldPanel):
**kwargs,
):
kwargs["classname"] = classname
kwargs.setdefault("required_on_save", True)
self.apply_if_live = apply_if_live
self.placeholder = placeholder
self.targets = targets

Wyświetl plik

@ -283,6 +283,7 @@ class Page(AbstractPage, index.Indexed, ClusterableModel, metaclass=PageBase):
max_length=255,
help_text=_("The page title as you'd like it to be seen by the public"),
)
title.required_on_save = True
# to reflect title of a current draft in the admin UI
draft_title = models.CharField(max_length=255, editable=False)
slug = models.SlugField(