Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for the issue: form validation message not working on unfocus #5321

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions widget/form.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package widget

import (
"errors"
"fmt"
"reflect"

"fyne.io/fyne/v2"
Expand Down Expand Up @@ -298,25 +299,38 @@ func (f *Form) isVertical() bool {
func (f *Form) setUpValidation(widget fyne.CanvasObject, i int) {
updateValidation := func(err error) {
if err == errFormItemInitialState {
return
return
}

f.Items[i].validationError = err
f.Items[i].invalid = err != nil
f.setValidationError(err)
f.checkValidation(err)
f.updateHelperText(f.Items[i])
f.setValidationError(err)
f.checkValidation(nil)
}

if w, ok := widget.(fyne.Validatable); ok {
f.Items[i].invalid = w.Validate() != nil

if e, ok := w.(*Entry); ok {
e.onFocusChanged = func(bool) {
updateValidation(e.validationError)
originalFocusChanged := e.onFocusChanged
e.onFocusChanged = func(focused bool) {
if originalFocusChanged != nil {
originalFocusChanged(focused)
}
if !focused {
validationErr := e.Validate()
fmt.Println("Validation triggered on focus loss:", validationErr)
e.SetValidationError(validationErr)
updateValidation(validationErr)
}
}

if e.Validator != nil && f.Items[i].invalid {
// set initial state error to guarantee next error (if triggers) is always different
e.SetValidationError(errFormItemInitialState)
}
}

w.SetOnValidationChanged(updateValidation)
}
}
Expand Down
Loading