Date Input with DateField

Live Demo

The DateField component provides the means to display and input dates. The field comes in two variations: DateField, with a numeric input box and a popup calendar view, and InlineDateField, with the calendar view always visible.

The example below illustrates the use of the DateField. We set the initial date of the date field to current date by using the factory method now of the java.time.LocalDate class.

Java

  1. // Create a DateField with the default style
  2. DateField date = new DateField();
  3. // Set the date to present
  4. date.setValue(LocalDate.now());

The result is shown in DateField for Selecting a Date.

datefield example1

DateField for Selecting a Date

DateField

The DateField provides date input using a text box. Clicking the handle right of the date opens a popup view for selecting the year, month, and day. The Down key also opens the popup. Once opened, the user can navigate the calendar using the cursor keys.

The date selected from the popup is displayed in the text box according to the default date and format of the current locale, or as specified with setDateFormat(). The same format definitions are used for parsing user input.

Date Format

The date is normally displayed according to the default format for the current locale (see “Locale”). You can specify a custom format with setDateFormat(). It takes a format string that follows the format of the java.time.format.DateTimeFormatter in Java.

Java

  1. // Display only year, month, and day in ISO format
  2. date.setDateFormat("yyyy-MM-dd");
  3. // Display the correct format as placeholder
  4. date.setPlaceholder("yyyy-mm-dd");

The result is shown in Custom Date Format for DateField.

datefield formatting

Custom Date Format for DateField

The same format specification is also used for parsing user-input date, as described later.

Handling Malformed User Input

A user can easily input a malformed or otherwise invalid date. DateField has two validation layers: first on the client-side and then on the server-side.

The validity of the entered date is first validated on the client-side, immediately when the input box loses focus. If the date format is invalid, the v-datefield-parseerror style is set. Whether this causes a visible indication of a problem depends on the theme. The built-in reindeer theme does not shown any indication by default, making server-side handling of the problem more convenient.

CSS

  1. .mydate.v-datefield-parseerror .v-textfield {
  2. background: pink;
  3. }

The setLenient(true) setting enables relaxed interpretation of dates, so that invalid dates, such as February 30th or March 0th, are wrapped to the next or previous month, for example.

The server-side validation phase occurs when the date value is sent to the server. If the date field is set in immediate state, it occurs immediately after the field loses focus. Once this is done and if the status is still invalid, an error indicator is displayed beside the component. Hovering the mouse pointer over the indicator shows the error message.

You can handle the errors by overriding the handleUnparsableDateString() method. The method gets the user input as a string parameter and can provide a custom parsing mechanism, as shown in the following example.

Java

  1. // Create a date field with a custom parsing and a
  2. // custom error message for invalid format
  3. DateField date = new DateField("My Date") {
  4. @Override
  5. protected Result<LocalDate> handleUnparsableDateString(
  6. String dateString) {
  7. try {
  8. // try to parse with alternative format
  9. LocalDate parsedAtServer = LocalDate.parse(dateString, DateTimeFormatter.ISO_DATE);
  10. return Result.ok(parsedAtServer);
  11. } catch (DateTimeParseException e) {
  12. return Result.error("Bad date");
  13. }
  14. }
  15. };
  16. // Display only year, month, and day in slash-delimited format
  17. date.setDateFormat("yyyy/MM/dd");
  18. // Don't be too tight about the validity of dates
  19. // on the client-side
  20. date.setLenient(true);

CSS Style Rules

CSS

  1. .v-datefield, v-datefield-popupcalendar {}
  2. .v-textfield, v-datefield-textfield {}
  3. .v-datefield-button {}

The top-level element of DateField and InlineDateField have v-datefield style. The DateField also has the v-datefield-popupcalendar style.

In addition, the top-level element has a style that indicates the resolution, with v-datefield- basename and an extension, which is one of full, day, month, or year. The -full style is enabled when the resolution is smaller than a day. These styles are used mainly for controlling the appearance of the popup calendar.

The text box has v-textfield and v-datefield-textfield styles, and the calendar button v-datefield-button.

Once opened, the calendar popup has the following styles at the top level:

CSS

  1. .v-datefield-popup {}
  2. .v-popupcontent {}
  3. .v-datefield-calendarpanel {}

The top-level element of the floating popup calendar has .v-datefield-popup style. Observe that the popup frame is outside the HTML structure of the component, hence it is not enclosed in the v-datefield element and does not include any custom styles. The content in the v-datefield-calendarpanel is the same as in InlineDateField, as described in InlineDateField.

InlineDateField

The InlineDateField provides a date picker component with a month view. The user can navigate months and years by clicking the appropriate arrows. Unlike with the pop-up variant, the month view is always visible in the inline field.

Java

  1. // Create a DateField with the default style
  2. InlineDateField date = new InlineDateField();
  3. // Set the date to present
  4. date.setValue(LocalDate.now());

The result is shown in Example of the InlineDateField.

datefield inlinedatefield

Example of the InlineDateField

The user can also navigate the calendar using the cursor keys.

CSS Style Rules

CSS

  1. .v-datefield {}
  2. .v-datefield-calendarpanel {}
  3. .v-datefield-calendarpanel-header {}
  4. .v-datefield-calendarpanel-prevyear {}
  5. .v-datefield-calendarpanel-prevmonth {}
  6. .v-datefield-calendarpanel-month {}
  7. .v-datefield-calendarpanel-nextmonth {}
  8. .v-datefield-calendarpanel-nextyear {}
  9. .v-datefield-calendarpanel-body {}
  10. .v-datefield-calendarpanel-weekdays,
  11. .v-datefield-calendarpanel-weeknumbers {}
  12. .v-first {}
  13. .v-last {}
  14. .v-datefield-calendarpanel-weeknumber {}
  15. .v-datefield-calendarpanel-day {}

The top-level element has the v-datefield style. In addition, the top-level element has a style name that indicates the resolution of the calendar, with v-datefield- basename and an extension, which is one of full, day, month, or year. The -full style is enabled when the resolution is smaller than a day.

The v-datefield-calendarpanel-weeknumbers and v-datefield-calendarpanel-weeknumber styles are enabled when the week numbers are enabled. The former controls the appearance of the weekday header and the latter the actual week numbers.

The other style names should be self-explanatory. For weekdays, the v-first and v-last styles allow making rounded endings for the weekday bar.

Date Resolution

In addition to display a calendar with dates, DateField can also display just the month or year. The visibility of the input components is controlled by setDateResolution(), which you can set with setResolution(). The method takes as its parameter the highest resolution date element that should be visible. Please see the API Reference for the complete list of resolution parameters.

DateField Locale

The date is displayed according to the locale of the user, as reported by the browser. You can set a custom locale with the setLocale() method of AbstractComponent, as described in “Locale”. Only Gregorian calendar is supported.

Week Numbers

You can enable week numbers in a date field with setShowISOWeekNumbers(). The numbers are shown according to the ISO 8601 standard in a column on the left side of the field.