From 23bd047c5fa93b99e1b211ba05eecbb3414f2902 Mon Sep 17 00:00:00 2001 From: Jacalz Date: Fri, 18 Aug 2023 14:01:12 +0200 Subject: [PATCH] Clean up and simplify form dialog code This was noticed as part of some research around #4147. The bug is still there but changes the behaviour now that we trust what the form is telling us about the validation status. Having it this way makes it easier to debug an actual fix for the bug. --- dialog/form.go | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/dialog/form.go b/dialog/form.go index bd12754ea1..61a3c4d801 100644 --- a/dialog/form.go +++ b/dialog/form.go @@ -28,24 +28,14 @@ func (d *FormDialog) Submit() { d.hideWithResponse(true) } -// validateItems acts as a validation edge state handler that will respond to an individual widget's validation -// state before checking all others to determine the net validation state. If the error passed is not nil, then the -// confirm button will be disabled. If the error parameter is nil, then all other Validatable widgets in items are -// checked as well to determine whether the confirm button should be disabled. -// This method is passed to each Validatable widget's SetOnValidationChanged method in items by NewForm. -func (d *FormDialog) validateItems(err error) { +// setSubmitState is intended to run when the form validation changes to +// enable/disable the submit button accordingly. +func (d *FormDialog) setSubmitState(err error) { if err != nil { d.confirm.Disable() return } - for _, item := range d.items { - if validatable, ok := item.Widget.(fyne.Validatable); ok { - if err := validatable.Validate(); err != nil { - d.confirm.Disable() - return - } - } - } + d.confirm.Enable() } @@ -74,9 +64,8 @@ func NewForm(title, confirm, dismiss string, items []*widget.FormItem, callback cancel: d.dismiss, } - formDialog.validateItems(nil) - - form.SetOnValidationChanged(formDialog.validateItems) + formDialog.setSubmitState(form.Validate()) + form.SetOnValidationChanged(formDialog.setSubmitState) d.create(container.NewGridWithColumns(2, d.dismiss, confirmBtn)) return formDialog