vaadin / flow-components

Java counterpart of Vaadin Web Components
101 stars 66 forks source link

Server-side date parsing #2466

Open rolfsmeds opened 2 years ago

rolfsmeds commented 2 years ago

Describe your motivation

Some applications use various specialized strings for quickly entering dates, such as +1 or tomorrow for tomorrow. While we can extend the parsing formats supported by Date Picker via date-fns in the future, it is likely that there will always be custom, application-specific strings that even date-fns does not support. Providing a solution for server-side parsing of dates, that allow developers to provide their own parsing, would solve these use cases.

Describe the solution you'd like

A way to provide server-side date parsing as a fallback (if the parsing formats provided are unable to parse the entered string). The API could be something along the lines of

DatePicker dp = new DatePicker();
dp.setFallbackParser(s -> {
  // custom logic for parsing the input string, e.g.
  if (s.equals("tomorrow")) {
    return LocalDate.now().plusDays(1);
  } else {
    return null;
}
knoobie commented 2 years ago

Flow has Converter (https://github.com/vaadin/flow/blob/master/flow-data/src/main/java/com/vaadin/flow/data/converter/Converter.java) for this kind of things - the generic lambda could probably be some kind of "one way" (or two way) Converter to allow the Result to contain an error for failed fallback parsing.


public void setFallbackParser(Converter<String, LocalDate> fallbackParser);

Edit: V8 had a similar handling with Result<LocalDate> as return parameter for handleUnparsableDateString ( https://github.com/vaadin/framework/blob/7dbb4f029f2802797f531abff4dc3cf5e336e53e/server/src/main/java/com/vaadin/ui/AbstractDateField.java#L896)

vursen commented 11 months ago

Here I described how this feature could potentially be implemented after the validation refactor is done.