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

set binding's value only after successful validation #5232

Open
2 tasks done
t-hofmann opened this issue Oct 30, 2024 · 1 comment
Open
2 tasks done

set binding's value only after successful validation #5232

t-hofmann opened this issue Oct 30, 2024 · 1 comment
Labels
Data binding Issue related to bug or improvement of data binding

Comments

@t-hofmann
Copy link

t-hofmann commented Oct 30, 2024

Checklist

  • 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

Typing a wrong value into an entry-field, which is bound to a variable will update the variable despite the entry's validator returning an error.

How to reproduce

The example offers two entry-field, each using the same validator.
The validator returns an error if the input is greater than 0.
The entry-fields use the same binding.

On input in any field the binding's Set method should only be called if the validation succeeds.

Currently the validator is seemingly only used to provide feedback to the user, while it could already prevent the propagation of a wrong value.

Screenshots

No response

Example code

package main

import (
	"fmt"
	"strconv"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/data/binding"
	"fyne.io/fyne/v2/widget"
)

func main() {

	app := app.New()
	w := app.NewWindow("Validate before calling binding's Set()")
	w.Resize(fyne.NewSize(800, 100))

	const MAX int = 0

	stringValidator := func(s string) error {
		v, err := strconv.Atoi(s)
		if err != nil {
			return err
		}

		if v > MAX {
			return fmt.Errorf("max: %d got: %d", MAX, v)
		}

		return nil
	}

	str := binding.IntToString(binding.NewInt())

	in1 := widget.NewEntryWithData(str)
	in1.Validator = stringValidator

	in2 := widget.NewEntryWithData(str)
	in2.Validator = stringValidator

	w.SetContent(container.NewVBox(
		in1,
		in2,
	))

	w.ShowAndRun()
}

Fyne version

2.5.2

Go compiler version

1.23.2

Operating system and version

Linuxmint 22

Additional Information

somehow related:
#1849
#1704

@t-hofmann t-hofmann added the unverified A bug that has been reported but not verified label Oct 30, 2024
@andydotxyz andydotxyz changed the title is:issue set binding's value only after successful validation set binding's value only after successful validation Dec 13, 2024
@andydotxyz
Copy link
Member

I think this makes sense.
However in the meantime you could put the validation in data binding not on the Entry which will force it to block binding propagation as the chain will error.
When you use data binding and replace Entry.Validator you are replacing the default validation that binding provides.

@andydotxyz andydotxyz reopened this Dec 13, 2024
@andydotxyz andydotxyz added Data binding Issue related to bug or improvement of data binding and removed unverified A bug that has been reported but not verified labels Dec 13, 2024
@andydotxyz andydotxyz added this to the "F" release, Early 2025 milestone Dec 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Data binding Issue related to bug or improvement of data binding
Projects
None yet
Development

No branches or pull requests

2 participants