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 validateable items not showing error text in some cases #5324

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
49 changes: 23 additions & 26 deletions widget/form.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,28 +158,22 @@
}

func (f *Form) createInput(item *FormItem) fyne.CanvasObject {
_, ok := item.Widget.(fyne.Validatable)
if item.HintText == "" {
if !ok {
return item.Widget
}
if !f.itemWidgetHasValidator(item.Widget) { // we don't have validation
return item.Widget
}
if item.helperOutput == nil {
// Always initialize helperOutput to show validation errors or hints
th := f.Theme()
v := fyne.CurrentApp().Settings().ThemeVariant()
text := canvas.NewText("", th.Color(theme.ColorNamePlaceHolder, v))
text.TextSize = th.Size(theme.SizeNameCaptionText)
item.helperOutput = text
}
f.updateHelperText(item) // Ensure the helper text is updated
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When methods are named clearly comments often add nothing


th := f.Theme()
v := fyne.CurrentApp().Settings().ThemeVariant()

text := canvas.NewText(item.HintText, th.Color(theme.ColorNamePlaceHolder, v))
text.TextSize = th.Size(theme.SizeNameCaptionText)
item.helperOutput = text
f.updateHelperText(item)
textContainer := &fyne.Container{Objects: []fyne.CanvasObject{text}}
// Wrap the input widget and helperOutput in a container
textContainer := &fyne.Container{Objects: []fyne.CanvasObject{item.helperOutput}}
return &fyne.Container{Layout: formItemLayout{form: f}, Objects: []fyne.CanvasObject{item.Widget, textContainer}}
}

func (f *Form) itemWidgetHasValidator(w fyne.CanvasObject) bool {

Check failure on line 176 in widget/form.go

View workflow job for this annotation

GitHub Actions / static_analysis

func (*Form).itemWidgetHasValidator is unused (U1000)
value := reflect.ValueOf(w).Elem()
validatorField := value.FieldByName("Validator")
if validatorField == (reflect.Value{}) {
Expand Down Expand Up @@ -344,23 +338,26 @@
}

func (f *Form) updateHelperText(item *FormItem) {
if item.helperOutput == nil {
return // Exit early if helperOutput is not initialized (shouldn't happen with the updated createInput)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this should not happen then is the line needed?

}

th := f.Theme()
v := fyne.CurrentApp().Settings().ThemeVariant()

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
}
if item.validationError == nil || showHintIfError {
if item.validationError != nil {
// Show the validation error message
item.helperOutput.Text = item.validationError.Error()
item.helperOutput.Color = th.Color(theme.ColorNameError, v)
} else if item.HintText != "" {
// Show the hint text if there's no validation error
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)
// Clear the text if neither validation error nor hint exists
item.helperOutput.Text = ""
}

item.helperOutput.Refresh()
}

Expand Down
Loading