Skip to content

Commit

Permalink
Remove the remaining use of hardcoded Entry widget in Form
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacalz committed Nov 8, 2023
1 parent e370066 commit 6f401ff
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
5 changes: 5 additions & 0 deletions validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ type ExpandedValidator interface {

// SetValidationError manually updates the validation status until the next input change.
SetValidationError(error)

// ShouldDisplayValidation tells a parent widget if the validatable widget wants to currently display
// the validation error. This can be used to avoid "nagging" the user about errors until it makes
// sense to show it. I.e. an empty widget.Entry that hasn't been interacted with does not show the error.
ShouldDisplayValidation() bool
}

// Validatable is an interface for specifying if a widget is validatable.
Expand Down
8 changes: 8 additions & 0 deletions widget/entry_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ func (e *Entry) SetValidationError(err error) {
}
}

// ShouldDisplayValidation returns true if the entry has not been interacted with
// or is currently focused.
//
// Since: 2.5
func (e *Entry) ShouldDisplayValidation() bool {
return !e.dirty || e.focused
}

var _ fyne.Widget = (*validationStatus)(nil)

type validationStatus struct {
Expand Down
8 changes: 4 additions & 4 deletions widget/form.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,10 +296,10 @@ func (f *Form) updateHelperText(item *FormItem) {
if item.helperOutput == nil {
return // testing probably, either way not rendered yet
}
showHintIfError := false
if e, ok := item.Widget.(*Entry); ok && (!e.dirty || e.focused) {
showHintIfError = true
}

ev, ok := item.Widget.(fyne.ExpandedValidator)
showHintIfError := ok && ev.ShouldDisplayValidation()

if item.validationError == nil || showHintIfError {
item.helperOutput.Text = item.HintText
item.helperOutput.Color = theme.PlaceHolderColor()
Expand Down

0 comments on commit 6f401ff

Please sign in to comment.