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

Entry validation does not work for empty field (?) #2179

Closed
JohannesHampel opened this issue Apr 17, 2021 · 2 comments
Closed

Entry validation does not work for empty field (?) #2179

JohannesHampel opened this issue Apr 17, 2021 · 2 comments
Labels
bug Something isn't working

Comments

@JohannesHampel
Copy link

Hi,

I'm using fyne 2.0.2, go 1.14 on macos. Sorry, if the question is studip, I am new to fyne and go:

I'd like to have an Entry where the entered value needs to have at least 2 characters. The validation works ok, if one inputs at least one character, but in case the Entry is left completely empty, no validation is done.

What I am doing wrong?

package main

import (
	"errors"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/layout"
	"fyne.io/fyne/v2/widget"
)

func main() {
	app := app.New()

	w := app.NewWindow("Hello")
	e := widget.NewEntry()
	e.Validator = func (str string) error {
		if len(e.Text)<2 {
			return errors.New("Too short")
		}
		return nil
	}

	content := container.New(layout.NewHBoxLayout(),  e)
	w.SetContent(content)

	w.ShowAndRun()
}
@JohannesHampel
Copy link
Author

In the meanwhile I figured out that this behavior seems to be intended:

From entry_validation.go

func (r *validationStatusRenderer) Refresh() {
	r.entry.propertyLock.RLock()
	defer r.entry.propertyLock.RUnlock()
	if r.entry.Text == "" || r.entry.disabled {
		r.icon.Hide()
		return
	}
        ...

I guess this is the part where fyne comes to the conclusion that it never renders the validation icon in case the Entry is empty.
So the question is: what is the best way to have an Entry that validates for required input?

@andydotxyz
Copy link
Member

I think you may have found a bug. The intention is that we do not show errors for validation before the field is edited (i.e. when it's still blank) but that editing the field, then putting it back to blank, then moving focus elsewhere the error should appear.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants