Skip to content

Commit

Permalink
Merge pull request fyne-io#2967 from Jacalz/validation++
Browse files Browse the repository at this point in the history
Various minor validation improvements
  • Loading branch information
Jacalz committed May 16, 2022
2 parents 6da7bbb + 23a0d89 commit 858209d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
3 changes: 2 additions & 1 deletion data/validation/regexp.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ func NewRegexp(regexpstr, reason string) fyne.StringValidator {
return nil
}

err = errors.New(reason)
return func(text string) error {
if expression != nil && !expression.MatchString(text) {
return errors.New(reason)
return err
}

return nil // Nothing to validate with, same as having no validator.
Expand Down
9 changes: 4 additions & 5 deletions widget/entry_validation.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package widget

import (
"errors"

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/theme"
Expand All @@ -22,9 +24,7 @@ func (e *Entry) Validate() error {
// SetOnValidationChanged is intended for parent widgets or containers to hook into the validation.
// The function might be overwritten by a parent that cares about child validation (e.g. widget.Form).
func (e *Entry) SetOnValidationChanged(callback func(error)) {
if callback != nil {
e.onValidationChanged = callback
}
e.onValidationChanged = callback
}

// SetValidationError manually updates the validation status until the next input change
Expand All @@ -36,8 +36,7 @@ func (e *Entry) SetValidationError(err error) {
return
}

if (err == nil && e.validationError != nil) || (e.validationError == nil && err != nil) ||
err.Error() != e.validationError.Error() {
if !errors.Is(err, e.validationError) {
e.validationError = err

if e.onValidationChanged != nil {
Expand Down
5 changes: 5 additions & 0 deletions widget/entry_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,9 @@ func TestEntry_SetOnValidationChanged(t *testing.T) {
modified = false
test.Type(entry, "-01-01")
assert.True(t, modified)

modified = false
entry.SetOnValidationChanged(nil)
test.Type(entry, "invalid")
assert.False(t, modified)
}

0 comments on commit 858209d

Please sign in to comment.