#5232·fyne

set binding's value only after successful validation

Author: t-hofmannCreated Oct 30, 2024Updated Sep 3, 2026
Labelsdata binding

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

go
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