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 validator not updating if content is changed via data binding after SetContent #2639

Closed
Cookie04DE opened this issue Nov 11, 2021 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@Cookie04DE
Copy link

Describe the bug:

If you create a form and put in an entry with data, set a custom validator and change the data after you set the content of the window the entry will display an error even though the content is correct according to the validator and you won't be to submit the form.

To Reproduce:

Steps to reproduce the behaviour:

  1. Copy the example code below
  2. Run it and see the red outlining and the inability to submit the form
  3. Swap the lines integerBind.Set(1)with w.SetContent(f)
  4. Run it again and see that it works now

Screenshots:

With the SetContent line before the Set line:
before
With the SetContent line after the Set line:
after

Example code:

package main

import (
	"fmt"
	"strconv"

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

func main() {
	a := app.New()
	w := a.NewWindow("Test")

	var integer int

	integerBind := binding.BindInt(&integer)

	dayEntry := widget.NewEntryWithData(binding.IntToString(integerBind))
	dayEntry.Validator = func(s string) error {
		i, err := strconv.Atoi(s)
		if err != nil {
			return fmt.Errorf("Must be an integer")
		}
		if i < 1 {
			return fmt.Errorf("Must be bigger than zero")
		}
		return nil
	}
	f := widget.NewForm(
		widget.NewFormItem("Integer", dayEntry),
	)
	f.SubmitText = "Submit"
	f.OnSubmit = func() {
	}
	w.SetContent(f)
	integerBind.Set(1) //works if this line is placed before the call to SetContent
	w.ShowAndRun()
}

Device (please complete the following information):

  • OS: MacOS
  • Version: Monterey 12.0.1
  • Go version: go1.17.1 darwin/arm64
  • Fyne version: 2.1.1
@Cookie04DE Cookie04DE added the unverified A bug that has been reported but not verified label Nov 11, 2021
@drognisep
Copy link
Contributor

I think we're running into the same thing, @Cookie04DE.

The screenshots show that the value in the Entry is updated, but the validator seems to not like it because the submit button is disabled. Changing the string in the form item in any way re-runs the validator and the submit button is enabled.

@andydotxyz or @Jacalz have you all had a chance to look at this? This code exhibits the same behavior.

package main

import (
	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/data/binding"
	"fyne.io/fyne/v2/data/validation"
	"fyne.io/fyne/v2/dialog"
	"fyne.io/fyne/v2/widget"
)

func main() {
	w := app.New().NewWindow("Testing form validation")
	w.CenterOnScreen()
	w.Resize(fyne.NewSize(300, 100))

	someString := ""
	boundString := binding.BindString(&someString)
	entry := widget.NewEntryWithData(boundString)
	entry.Validator = validation.NewRegexp(`^[a-z]+$`, "Must be lowercase characters")
	item := widget.NewFormItem("Some String", entry)
	form := widget.NewForm(item)

	form.OnSubmit = func() {
		dialog.ShowInformation("It worked!", "It worked!", w)
	}

	w.SetContent(form)
	_ = boundString.Set("test")
	w.ShowAndRun()
}

image
image
image

Using a form dialog instead shows the same behavior.

image

@drognisep
Copy link
Contributor

Interesting! It seems that if the value is already valid, then the entry is still shown as valid even if it's set to a value that would not be accepted by the validator. In my code above I initialized someString to "a" and used _ = boundString.Set("123") instead.

image

So the root of this seems to be that the validator is not necessarily called when a binding is Set.

@andydotxyz andydotxyz added bug Something isn't working and removed unverified A bug that has been reported but not verified labels Nov 29, 2021
@andydotxyz andydotxyz added this to the Fixes (v2.1.x) milestone Nov 29, 2021
@andydotxyz andydotxyz self-assigned this Nov 29, 2021
@andydotxyz
Copy link
Member

I am pretty sure this has been resolved on release/v2.1.x through work on another bug. I could replicate it before, but cannot now.

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

3 participants