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

Conditional Date Validation in Flutter #473

Open
dmarotta77 opened this issue Dec 20, 2024 · 1 comment · May be fixed by #476
Open

Conditional Date Validation in Flutter #473

dmarotta77 opened this issue Dec 20, 2024 · 1 comment · May be fixed by #476
Labels
question Further information is requested

Comments

@dmarotta77
Copy link

I need to specify start and end dates for an event with the following constraints:

  • Start date field is mandatory.
  • End date field is optional.
  • If the end date field is populated, it must be greater than or equal to the start date.

Currently, using Validators.compare, I'm getting an error notification even if the end date is not specified.

Should I implement a custom validator?

@dmarotta77 dmarotta77 added the question Further information is requested label Dec 20, 2024
@omj-denis omj-denis linked a pull request Jan 13, 2025 that will close this issue
4 tasks
@omj-denis
Copy link

omj-denis commented Jan 13, 2025

I'm in the same situation right now: min and max price filters, both optional. But if both are specified, a validation is needed (min <= max). So, I use CompareValidator for this, but it reports an error if one of the 2 fields is empty.

I see the following options:

  • CompareValidator should ignore nulls. If somebody needs to ensure non-null values, they can add Validators.required. That could be the simplest solution, but, unfortunately, it's a breaking change now.
  • custom validator, as you suggest. A duplication of CompareValidator. Duplication is not a good practice. Obviously, that's the reason why you want to avoid it and why you created this issue.
  • combine CompareValidator inside Validators.composeOR with a custom AllowNullValidator which only purpose is to report VALID for null values (and thus suppress the error coming from CompareValidator). This solution is acceptable but AllowNullValidator doesn't make much sense on its own.
  • add an optional parameter allowNull to CompareValidator to suppress this error (similar to PR allowNull flag for number validator #451)

Here's the implementation of the last option: PR #476

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

Successfully merging a pull request may close this issue.

2 participants