You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have searched the issue tracker for open issues that relate to the same problem, before opening a new one.
This issue only relates to a single bug. I will open new issues for any other problems.
Describe the bug
When creating a widget.Form if the widget satisfies the widget the fyne.Validatable then it should show red error text if the validation fails.
However this only happens for widget.Entry or with form items with HintText set.
How to reproduce
First run the program below. You will see that it creates a form with two validated items. The plain one works fine, but the wrapped one does not - the error text does not appear.
If you then uncomment the line //formItem.HintText = "Hint" // Hint needs to be not "" to make validation appear then the error text will appear.
PS Note you need to type something in the box and delete it to get the error message to appear - not sure if this is working as intended or not!
Screenshots
Form without HintText set - notice the validation line is not appearing for the "with help" widget
Now with HintText set
And showing the error
Example code
package main
import (
"errors""fmt""fyne.io/fyne/v2""fyne.io/fyne/v2/app""fyne.io/fyne/v2/container""fyne.io/fyne/v2/theme""fyne.io/fyne/v2/widget"
)
// Wrap any widget with a help button next to ittypeWithHelpstruct {
widget.BaseWidgetoriginalWidget fyne.CanvasObjecthelpButton*widget.Buttoncontent*fyne.Container
}
// check interfacesvar_ fyne.Validatable=&WithHelp{}
// Create a new widget with a help button next to it//// Aims to preserve the Validateable status of the wrapped widgetfuncNewWithHelp(originalWidget fyne.CanvasObject, helpTextstring) *WithHelp {
h:=&WithHelp{
originalWidget: originalWidget,
helpButton: widget.NewButtonWithIcon("", theme.HelpIcon(), func() {
fyne.CurrentApp().SendNotification(&fyne.Notification{
Title: "Help",
Content: helpText,
})
}),
}
h.content=container.NewBorder(nil, nil, nil, h.helpButton, h.originalWidget, h.helpButton)
h.ExtendBaseWidget(h)
returnh
}
// Render the combined originalWidget and helpButtonfunc (h*WithHelp) CreateRenderer() fyne.WidgetRenderer {
returnwidget.NewSimpleRenderer(h.content)
}
// Forward Validate if possible to originalWidgetfunc (h*WithHelp) Validate() error {
ifdo, ok:=h.originalWidget.(fyne.Validatable); ok {
err:=do.Validate()
h.Refresh() // Update the widget's appearance based on validation statereturnerr
}
returnnil
}
// Forward SetOnValidationChanged if possible to originalWidgetfunc (h*WithHelp) SetOnValidationChanged(callbackfunc(error)) {
ifdo, ok:=h.originalWidget.(fyne.Validatable); ok {
do.SetOnValidationChanged(func(errerror) {
callback(err)
h.Refresh() // Update the widget's appearance when validation changes
})
}
}
funcmain() {
myApp:=app.New()
myWindow:=myApp.NewWindow("Form test with Validateable widget")
// Create an entry widget with validation then wrap it in a WithHelpentry:=widget.NewEntry()
entry.Validator=func(valstring) error {
ifval=="" {
returnerrors.New("Can't be empty")
}
returnnil
}
withHelp:=NewWithHelp(entry, "This is help text for item1")
// Create an plain entry widget with validationentryPlain:=widget.NewEntry()
entryPlain.Validator=func(valstring) error {
ifval=="" {
returnerrors.New("Can't be empty")
}
returnnil
}
formItem:=widget.NewFormItem("with help", withHelp)
//formItem.HintText = "Hint" // Hint needs to be not "" to make validation appearplainFormItem:=widget.NewFormItem("plain", entryPlain)
form:=widget.NewForm(
formItem,
plainFormItem,
)
form.OnSubmit=func() {
fmt.Println("Submit")
}
form.OnCancel=func() {
myWindow.Close()
fmt.Println("Cancel")
}
myWindow.SetContent(container.NewVBox(form))
myWindow.Resize(fyne.NewSize(400, 10))
myWindow.ShowAndRun()
}
I looked at trying to fix this. I think this is caused by the specific casts to *Entry here
You're probably right - there are other tickets open about how Validation has some Entry special code.
I think there was a prior PR for a related fix that someone opened but did not complete. Could be good to resolve this as suggested above before the larger validation fixes get revisited
Checklist
Describe the bug
When creating a widget.Form if the widget satisfies the widget the
fyne.Validatable
then it should show red error text if the validation fails.However this only happens for
widget.Entry
or with form items withHintText
set.How to reproduce
First run the program below. You will see that it creates a form with two validated items. The plain one works fine, but the wrapped one does not - the error text does not appear.
If you then uncomment the line
//formItem.HintText = "Hint" // Hint needs to be not "" to make validation appear
then the error text will appear.PS Note you need to type something in the box and delete it to get the error message to appear - not sure if this is working as intended or not!
Screenshots
Form without
HintText
set - notice the validation line is not appearing for the "with help" widgetNow with
HintText
setAnd showing the error
Example code
Fyne version
7d81356 (master)
Also replicates on
1325f0d (develop)
Go compiler version
go1.23.2
Operating system and version
Ubuntu 22.04.5 LTS
Additional Information
I looked at trying to fix this. I think this is caused by the specific casts to
*Entry
herefyne/widget/form.go
Lines 353 to 356 in 7d81356
But I'm not confident enough I understand the problem to fix it, but willing to have a go given a hint!
The text was updated successfully, but these errors were encountered: