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 Broken when using Data binding #1890

Closed
AlbinoGeek opened this issue Feb 2, 2021 · 6 comments
Closed

Entry Validation Broken when using Data binding #1890

AlbinoGeek opened this issue Feb 2, 2021 · 6 comments
Labels
blocker Items that would block a forthcoming release bug Something isn't working

Comments

@AlbinoGeek
Copy link
Contributor

Describe the bug:

Adding a Validator to an Entry that does not belong to a form appears to have no effect on the visual validation of said field, so the point where returning an error from the StringValidator results in a checkmark being shown as if the field passed validation.

To Reproduce:

Steps to reproduce the behaviour:

  1. Use the provided example code
  2. Enter any values into the Entry
  3. Bug

Screenshots:

image

Example code:

package main

import (
	"fmt"
	"time"

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

var ukDateWithDecimals = "02.01.2006"
var errDateFormat = fmt.Errorf("date must match format: %v", ukDateWithDecimals)

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

	d1 := binding.NewString()
	d2 := binding.NewString()

	t1 := widget.NewEntryWithData(d1)
	t2 := widget.NewEntryWithData(d2)

	// setup Validation on the Entry(s)
	dateValidator := func(s string) (err error) {
		if _, err := time.Parse(ukDateWithDecimals, s); err != nil {
			fmt.Println("Validation Error:", errDateFormat)
			return errDateFormat
		}

		return nil
	}

	t1.Validator = dateValidator
	t2.Validator = dateValidator

	options := []string{
		"one-way flight",
		"return flight",
	}
	combo := widget.NewSelect(options, func(s string) {
		if s == options[0] {
			t2.Disable()
		} else {
			t2.Enable()
		}
	})

	bookPressed := func() {
		msg := ""

		if to := t2.Text; to == "" {
			msg = fmt.Sprintf("Flight booked on %s.", t1.Text)
		} else {
			msg = fmt.Sprintf("Flights booked on %s and %s.", t1.Text, to)
		}

		dialog.ShowInformation("Flight Booked", msg, w)
	}

	// Technically, a `widget.Form` should be used to create this interface,
	// however, this would not "be true" to the example shown in 7guis. The
	// code to use a Form is included for completeness however.
	//
	// form := widget.NewForm(
	// 	widget.NewFormItem("", combo),
	// 	widget.NewFormItem("From", t1),
	// 	widget.NewFormItem("To", t2),
	// )
	// form.SubmitText = "Book"
	// form.OnSubmit = bookPressed
	// w.SetContent(form)
	//
	// If using the form, comment out the button and w.SetContent used below:

	button := widget.NewButton("Book", bookPressed)
	w.SetContent(container.NewVBox(combo, t1, t2, button))
	w.Resize(w.Content().MinSize().Add(fyne.NewSize(80, 0)))
	w.ShowAndRun()
}

Device (please complete the following information):

  • OS: Fedora Linux
  • Version: 33 Workstation
  • Go version: 1.15.6 linux/amd64
  • Fyne version: 2.0.0
@AlbinoGeek AlbinoGeek added the bug Something isn't working label Feb 2, 2021
@andydotxyz andydotxyz added the blocker Items that would block a forthcoming release label Feb 2, 2021
@AlbinoGeek
Copy link
Contributor Author

Even calling .SetValidationError(err) on the entry did not cause the field to go into the "error" state.

Simplified code that still reproduces this:

package main

import (
	"fmt"

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

func main() {
	a := app.New()
	w := a.NewWindow("Entry Error")
	data := binding.NewString()
	entry := widget.NewEntryWithData(data)

	entry.Validator = func(s string) (err error) {
		err = fmt.Errorf("this is an error")
		entry.SetValidationError(err)
		return
	}

	w.SetContent(entry)

	w.Resize(w.Content().MinSize().Add(fyne.NewSize(80, 0)))

	w.ShowAndRun()
}

@andydotxyz
Copy link
Member

Ah data binding. I see now. Maybe the data binding error is stomping over the validation that is set?

@andydotxyz andydotxyz changed the title Entry Validation Broken? Entry Validation Broken when using Data binding Feb 3, 2021
andydotxyz added a commit that referenced this issue Feb 4, 2021
@andydotxyz
Copy link
Member

fixed on develop :)

@kvishal7
Copy link

kvishal7 commented Jan 2, 2024

is this fixed in master ?
I still see this issue in 2.4.0

@andydotxyz
Copy link
Member

It was fixed back at v2.1.0 - are you certain you are seeing the exact same issue?

@kvishal7
Copy link

kvishal7 commented Jan 3, 2024

@andydotxyz I was seeing the exact same issue. I used a workaround where I extract the data from the entry directly instead of the binding. But now when I revert the code to the one with binding, the vaildation works!
maybe there was sth else which broke the validation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocker Items that would block a forthcoming release bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants