Defer validation on FieldPanel unless required_on_save passed

pull/12964/head
Matt Westcott 2025-02-24 17:48:16 +00:00 zatwierdzone przez Matt Westcott
rodzic d0e106b403
commit a56888a7ab
4 zmienionych plików z 14 dodań i 1 usunięć

Wyświetl plik

@ -25,6 +25,7 @@ class FieldPanel(Panel):
disable_comments=None,
permission=None,
read_only=False,
required_on_save=False,
**kwargs,
):
super().__init__(**kwargs)
@ -33,6 +34,7 @@ class FieldPanel(Panel):
self.disable_comments = disable_comments
self.permission = permission
self.read_only = read_only
self.required_on_save = required_on_save
def clone_kwargs(self):
kwargs = super().clone_kwargs()
@ -42,6 +44,7 @@ class FieldPanel(Panel):
disable_comments=self.disable_comments,
permission=self.permission,
read_only=self.read_only,
required_on_save=self.required_on_save,
)
return kwargs
@ -52,6 +55,9 @@ class FieldPanel(Panel):
opts = {
"fields": [self.field_name],
}
if not self.required_on_save:
opts["defer_required_on_fields"] = [self.field_name]
if self.widget:
opts["widgets"] = {self.field_name: self.widget}

Wyświetl plik

@ -32,6 +32,7 @@ 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

@ -151,6 +151,8 @@ class CreateView(WagtailAdminTemplateMixin, HookResponseMixin, View):
parent_page=self.parent_page,
for_user=self.request.user,
)
if self.action_name == "save":
self.form.defer_required_fields()
if self.form.is_valid():
return self.form_valid(self.form)
@ -243,7 +245,7 @@ class CreateView(WagtailAdminTemplateMixin, HookResponseMixin, View):
self.set_default_privacy_setting()
# Save revision
self.page.save_revision(user=self.request.user, log_action=True)
self.page.save_revision(user=self.request.user, log_action=True, clean=False)
# Save subscription settings
self.subscription.page = self.page

Wyświetl plik

@ -489,6 +489,9 @@ class EditView(WagtailAdminTemplateMixin, HookResponseMixin, View):
for_user=self.request.user,
)
if self.action_name == "save":
self.form.defer_required_fields()
if self.form.is_valid() and not self.locked_for_user:
return self.form_valid(self.form)
else:
@ -551,6 +554,7 @@ class EditView(WagtailAdminTemplateMixin, HookResponseMixin, View):
user=self.request.user,
log_action=True, # Always log the new revision on edit
previous_revision=self.previous_revision,
clean=False,
)
self.add_save_confirmation_message()