Skip to content

Entry Validation Broken when using Data binding #1890

Closed
@AlbinoGeek

Description

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    blockerItems that would block a forthcoming releasebugSomething isn't working

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions