Skip to content

Conversation

arieroos
Copy link

Description:

I changed the validation behaviour of DateEntry, so that the default Validator is only set if the user code does not provide it's own Validator

As part of this change:

  • The default validator now allows for an empty date. This better aligns with how I understand the documentation for SetDate, which gave me the impression that it should be possible to unselect the date.
  • DateEntry now has a DateFormat() function, allowing user code to know which date format is being used. This is useful for validation.
  • DateEntry now have tests

Fixes #5865

Checklist:

  • Tests included.
  • Lint and formatter run with no errors. *
  • Tests all pass.

* gofmt and goimport passes without any changes. I am not sure which linter to use, but my editor is set up to use gopls for pretty much everything

Where applicable:

  • Public APIs match existing style and have Since: line. **
  • Any breaking changes have a deprecation path or have been discussed. ***

** I added a function name DateFormat to DateEntry. I am not sure what I should put in the Since tag?
*** I changed the way the default Validator works for DateEntry. I am happy to revert this if it is considered to be a breaking change.

@arieroos arieroos marked this pull request as ready for review July 18, 2025 10:14
// this also the format in which date srtings are passed to the validator.
//
// Since: TODO: I am unsure what to put here.
func (e *DateEntry) DateFormat() string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this makes more sense to be a new public API on the fyne.Locale type, rather than on Entry. And FYI the Since line would be // Since: 2.7 in this case, since 2.7 will be the next feature release of Fyne

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that it makes more sense. However, the logic that I call for that method is in the widget package (under widget/locale.go). I can move the logic to the root fyne package, which would also improve fyne's current locale functionality. I just didn't want to take the liberty to make architectural decisions like that.

@andydotxyz
Copy link
Member

I'm unclear why the new DateFormat function is needed at all - if the intent was to override validator then the formatting seems tangencial. If the source problem was the format then the solution may look different.

For clarity can you describe the original issue that this PR is resolving?

Thanks for the contribution :)

@arieroos
Copy link
Author

The original issue I had was that I couldn't clear the DateEntry without the default validator invalidating the field. This meant that I could not make an optional date input. I also have the need to check that the date is after a certain point in time. I can think of more examples, there are probably infinite use cases for custom date validation.

I was surprised when I couldn't just set the Validator field on the DateEntry and have that validator be used. Which is why I consider this to be a bug.

The user code needs to know which DateFormat the input is using, in order to parse the string which is passed to the Validator. The string needs to be parsed so that we can then run checks on the date which the user supplied.

Another solution to this could be ad a Validator kind of field that takes in a func(*time.Time) bool. But I thought that might be a bigger change, and doesn't fix the unexpected behaviour of a public field not having any effect.

@andydotxyz
Copy link
Member

The original issue I had was that I couldn't clear the DateEntry without the default validator invalidating the field.

Yup, let's get that fixed for sure.

I also have the need to check that the date is after a certain point in time

Isn't that possible with the builtin validator?

The user code needs to know which DateFormat the input is using, in order to parse the string which is passed to the Validator.

This is a side-effect of replacing the validator - if you call the builtin one it won't be required.

and doesn't fix the unexpected behaviour of a public field not having any effect.

Yes, that should be resolved as well - but you could chain your new validator on top of the old one...

@arieroos
Copy link
Author

It feels like I am missing something crucial in how to use the Validator.

Isn't that possible with the builtin validator?

Not in a way that I could figure out. As far as I know, the default Validator just checks whether it can parse the input to a date, and returns an error if it can't.

you could chain your new validator on top of the old one.

I am not sure about how I would go about achieving this. The old Validator is instantiated as part of CreateRenderer.
The only way I can think of doing that, is by validating the date in OnChanged and using SetValidationError. Is that the way I am meant to do it? Or am I missing something?

@andydotxyz
Copy link
Member

Isn't that possible with the builtin validator?

Not in a way that I could figure out. As far as I know, the default Validator just checks whether it can parse the input to a date, and returns an error if it can't.

Oh on reflection, I see you meant that you wanted to validate that the parsed date is after some date. I didn't get that before.
Given that won't you want to pass the default validator that handles parsing and then use the parsed time and do the comparison? Delegation should work nicely.

I am not sure about how I would go about achieving this. The old Validator is instantiated as part of CreateRenderer.

Fair, I missed that. You can call Refresh() first which will cause the renderer to be created. Not ideal but saves creating new APis.
Perhaps the cleaner solution is that we make a DateValidator - the toolkit can work out what the format would be from the locale without new public APIs on DateEntry...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants