Skip to content

Commit

Permalink
Hide empty spaces where validation is enabled but nothing to show (#5485
Browse files Browse the repository at this point in the history
)
  • Loading branch information
andydotxyz authored Feb 3, 2025
1 parent 739f0e6 commit 5ebf631
Show file tree
Hide file tree
Showing 13 changed files with 32 additions and 4 deletions.
3 changes: 2 additions & 1 deletion widget/entry_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,10 @@ func (e *Entry) setValidationError(err error) bool {
return false
}

changed := e.validationError != err
e.validationError = err

if e.onValidationChanged != nil {
if e.onValidationChanged != nil && changed {
e.onValidationChanged(err)
}

Expand Down
25 changes: 22 additions & 3 deletions widget/form.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type FormItem struct {
validationError error
invalid bool
helperOutput *canvas.Text
wasFocused bool
}

// NewFormItem creates a new form item with the specified label text and input widget
Expand Down Expand Up @@ -351,16 +352,28 @@ func (f *Form) updateHelperText(item *FormItem) {
return // testing probably, either way not rendered yet
}
showHintIfError := false
if e, ok := item.Widget.(*Entry); ok && (!e.dirty || e.focused) {
showHintIfError = true
if e, ok := item.Widget.(*Entry); ok {
if !e.dirty || (e.focused && !item.wasFocused) {
showHintIfError = true
}
if e.dirty && !e.focused {
item.wasFocused = true
}
}

if item.validationError == nil || showHintIfError {
item.helperOutput.Text = item.HintText
item.helperOutput.Color = th.Color(theme.ColorNamePlaceHolder, v)
} else {
item.helperOutput.Text = item.validationError.Error()
item.helperOutput.Color = th.Color(theme.ColorNameError, v)
}

if item.helperOutput.Text == "" {
item.helperOutput.Hide()
} else {
item.helperOutput.Show()
}
item.helperOutput.Refresh()
}

Expand Down Expand Up @@ -472,5 +485,11 @@ func (f formItemLayout) MinSize(objs []fyne.CanvasObject) fyne.Size {
min1 := objs[1].MinSize()

minWidth := fyne.Max(min0.Width, min1.Width)
return fyne.NewSize(minWidth, min0.Height+min1.Height+innerPad)
height := min0.Height

items := objs[1].(*fyne.Container).Objects
if len(items) > 0 && items[0].(*canvas.Text).Text != "" {
height += min1.Height + innerPad
}
return fyne.NewSize(minWidth, height)
}
8 changes: 8 additions & 0 deletions widget/form_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,15 @@ func TestForm_Hints(t *testing.T) {
w := test.NewWindow(form)
defer w.Close()

w.Resize(form.MinSize().Add(fyne.NewSquareSize(theme.Padding() * 2)))
test.AssertImageMatches(t, "form/hint_initial.png", w.Canvas().Capture())

test.Type(entry2, "n")
w.Resize(form.MinSize().Add(fyne.NewSquareSize(theme.Padding() * 2)))
test.AssertImageMatches(t, "form/hint_invalid.png", w.Canvas().Capture())

test.Type(entry2, "ot-")
w.Resize(form.MinSize().Add(fyne.NewSquareSize(theme.Padding() * 2)))
test.AssertImageMatches(t, "form/hint_valid.png", w.Canvas().Capture())
}

Expand Down Expand Up @@ -301,24 +304,29 @@ func TestForm_Disable_Validation(t *testing.T) {
w := test.NewWindow(form)
defer w.Close()

w.Resize(form.MinSize().Add(fyne.NewSquareSize(theme.Padding() * 2)))
test.AssertImageMatches(t, "form/disable_validation_initial.png", w.Canvas().Capture())

form.Disable()

w.Resize(form.MinSize().Add(fyne.NewSquareSize(theme.Padding() * 2)))
test.AssertImageMatches(t, "form/disable_validation_disabled_invalid.png", w.Canvas().Capture())

form.Enable()

w.Resize(form.MinSize().Add(fyne.NewSquareSize(theme.Padding() * 2)))
test.AssertImageMatches(t, "form/disable_validation_enabled_invalid.png", w.Canvas().Capture())

entry.SetText("15-true")
w.Resize(form.MinSize().Add(fyne.NewSquareSize(theme.Padding() * 2)))
test.AssertImageMatches(t, "form/disable_validation_enabled_valid.png", w.Canvas().Capture())

// ensure we don't re-enable the form when entering something valid
entry.SetText("invalid")
form.Disable()
entry.SetText("15-true")

w.Resize(form.MinSize().Add(fyne.NewSquareSize(theme.Padding() * 2)))
test.AssertImageMatches(t, "form/disable_validation_disabled_valid.png", w.Canvas().Capture())
}

Expand Down
Binary file modified widget/testdata/form/disable_validation_disabled_valid.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified widget/testdata/form/disable_validation_enabled_valid.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified widget/testdata/form/hint_invalid.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified widget/testdata/form/hint_valid.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified widget/testdata/form/validation_entry_first_type_initial.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified widget/testdata/form/validation_entry_first_type_invalid.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified widget/testdata/form/validation_entry_first_type_valid.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified widget/testdata/form/validation_initial.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified widget/testdata/form/validation_invalid.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified widget/testdata/form/validation_valid.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 5ebf631

Please sign in to comment.