Skip to content

Commit

Permalink
Clean up and simplify form dialog code
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Jacalz committed Aug 18, 2023
1 parent 580211a commit 23bd047
Showing 1 changed file with 6 additions and 17 deletions.
23 changes: 6 additions & 17 deletions dialog/form.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 23bd047

Please sign in to comment.