You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

components-datefield.asciidoc 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. ---
  2. title: Date Input with DateField
  3. order: 13
  4. layout: page
  5. ---
  6. [[components.datefield]]
  7. = Date Input with DateField
  8. ifdef::web[]
  9. [.sampler]
  10. image:{live-demo-image}[alt="Live Demo", link="http://demo.vaadin.com/sampler/#ui/data-input/dates/date-field"]
  11. endif::web[]
  12. The [classname]#DateField# component provides the means to display and input
  13. dates. The field comes in two variations: [classname]#DateField#,
  14. with a numeric input box and a popup calendar view, and
  15. [classname]#InlineDateField#, with the calendar view always visible.
  16. The example below illustrates the use of the [classname]#DateField#. We set the initial date
  17. of the date field to current date by using the factory method [methodname]#now#
  18. of the [classname]#java.time.LocalDate# class.
  19. [source, java]
  20. ----
  21. // Create a DateField with the default style
  22. DateField date = new DateField();
  23. // Set the date to present
  24. date.setValue(LocalDate.now());
  25. ----
  26. The result is shown in <<figure.components.datefield.basic>>.
  27. [[figure.components.datefield.basic]]
  28. .[classname]#DateField# for Selecting a Date
  29. image::img/datefield-example1.png[width=35%, scaledwidth=60%]
  30. [[components.datefield.popupdatefield]]
  31. == [classname]#DateField#
  32. The [classname]#DateField# provides date input using a text box. Clicking the handle right of the date opens a popup view
  33. for selecting the year, month, and day. The Down key also opens the popup.
  34. Once opened, the user can navigate the calendar using the cursor keys.
  35. The date selected from the popup is displayed in the text box according to the
  36. default date and format of the current locale, or as specified with [methodname]#setDateFormat()#.
  37. The same format definitions are used for parsing user input.
  38. [[components.datefield.popupdatefield.format]]
  39. === Date Format
  40. The date is normally displayed according to the default format for the current locale (see
  41. <<dummy/../../../framework/components/components-features#components.features.locale,"Locale">>).
  42. You can specify a custom format with [methodname]#setDateFormat()#. It takes a
  43. format string that follows the format of the [classname]#java.time.format.DateTimeFormatter# in
  44. Java.
  45. [source, java]
  46. ----
  47. // Display only year, month, and day in ISO format
  48. date.setDateFormat("yyyy-MM-dd");
  49. // Display the correct format as placeholder
  50. date.setPlaceholder("yyyy-mm-dd");
  51. ----
  52. The result is shown in <<figure.components.datefield.popupdatefield.format>>.
  53. [[figure.components.datefield.popupdatefield.format]]
  54. .Custom Date Format for [classname]#DateField#
  55. image::img/datefield-formatting.png[width=35%, scaledwidth=60%]
  56. The same format specification is also used for parsing user-input date,
  57. as described later.
  58. [[components.datefield.popupdatefield.malformed]]
  59. === Handling Malformed User Input
  60. A user can easily input a malformed or otherwise invalid date.
  61. [classname]#DateField# has two validation layers: first on the client-side and
  62. then on the server-side.
  63. The validity of the entered date is first validated on the client-side,
  64. immediately when the input box loses focus. If the date format is invalid, the
  65. [literal]#++v-datefield-parseerror++# style is set. Whether this causes a
  66. visible indication of a problem depends on the theme. The built-in
  67. [literal]#++reindeer++# theme does not shown any indication by default, making
  68. server-side handling of the problem more convenient.
  69. [source, css]
  70. ----
  71. .mydate.v-datefield-parseerror .v-textfield {
  72. background: pink;
  73. }
  74. ----
  75. The [methodname]#setLenient(true)# setting enables relaxed interpretation of
  76. dates, so that invalid dates, such as February 30th or March 0th, are wrapped to
  77. the next or previous month, for example.
  78. The server-side validation phase occurs when the date value is sent to the
  79. server. If the date field is set in immediate state, it occurs immediately after
  80. the field loses focus. Once this is done and if the status is still invalid, an
  81. error indicator is displayed beside the component. Hovering the mouse pointer
  82. over the indicator shows the error message.
  83. You can handle the errors by overriding the
  84. [methodname]#handleUnparsableDateString()# method. The method gets the user
  85. input as a string parameter and can provide a custom parsing mechanism, as shown
  86. in the following example.
  87. [source, java]
  88. ----
  89. // Create a date field with a custom parsing and a
  90. // custom error message for invalid format
  91. DateField date = new DateField("My Date") {
  92. @Override
  93. protected Result<LocalDate> handleUnparsableDateString(
  94. String dateString) {
  95. try {
  96. // try to parse with alternative format
  97. LocalDate parsedAtServer = LocalDate.parse(dateString, DateTimeFormatter.ISO_DATE);
  98. return Result.ok(parsedAtServer);
  99. } catch (DateTimeParseException e) {
  100. return Result.error("Bad date");
  101. }
  102. }
  103. };
  104. // Display only year, month, and day in slash-delimited format
  105. date.setDateFormat("yyyy/MM/dd");
  106. // Don't be too tight about the validity of dates
  107. // on the client-side
  108. date.setLenient(true);
  109. ----
  110. [[components.datefield.popupdatefield.css]]
  111. === CSS Style Rules
  112. [source, css]
  113. ----
  114. .v-datefield, v-datefield-popupcalendar {}
  115. .v-textfield, v-datefield-textfield {}
  116. .v-datefield-button {}
  117. ----
  118. The top-level element of [classname]#DateField# and [classname]#InlineDateField# have
  119. [literal]#++v-datefield++# style. The [classname]#DateField# also has the
  120. [literal]#++v-datefield-popupcalendar++# style.
  121. In addition, the top-level element has a style that indicates the resolution,
  122. with [literal]#++v-datefield-++# basename and an extension, which is one of
  123. [literal]#++full++#, [literal]#++day++#, [literal]#++month++#, or
  124. [literal]#++year++#. The [literal]#++-full++# style is enabled when the
  125. resolution is smaller than a day. These styles are used mainly for controlling
  126. the appearance of the popup calendar.
  127. The text box has [literal]#++v-textfield++# and
  128. [literal]#++v-datefield-textfield++# styles, and the calendar button
  129. [literal]#++v-datefield-button++#.
  130. Once opened, the calendar popup has the following styles at the top level:
  131. [source, css]
  132. ----
  133. .v-datefield-popup {}
  134. .v-popupcontent {}
  135. .v-datefield-calendarpanel {}
  136. ----
  137. The top-level element of the floating popup calendar has
  138. [literal]#++.v-datefield-popup++# style. Observe that the popup frame is outside
  139. the HTML structure of the component, hence it is not enclosed in the
  140. [literal]#++v-datefield++# element and does not include any custom styles.
  141. // NOTE: May be changed in #5752.
  142. The content in the [literal]#++v-datefield-calendarpanel++# is the same as in
  143. [classname]#InlineDateField#, as described in <<components.datefield.calendar>>.
  144. [[components.datefield.calendar]]
  145. == [classname]#InlineDateField#
  146. The [classname]#InlineDateField# provides a date picker component with a month
  147. view. The user can navigate months and years by clicking the appropriate arrows.
  148. Unlike with the pop-up variant, the month view is always visible in the inline
  149. field.
  150. [source, java]
  151. ----
  152. // Create a DateField with the default style
  153. InlineDateField date = new InlineDateField();
  154. // Set the date to present
  155. date.setValue(LocalDate.now());
  156. ----
  157. The result is shown in <<figure.components.datefield.inlinedatefield>>.
  158. [[figure.components.datefield.inlinedatefield]]
  159. .Example of the [classname]#InlineDateField#
  160. image::img/datefield-inlinedatefield.png[width=35%, scaledwidth=60%]
  161. The user can also navigate the calendar using the cursor keys.
  162. === CSS Style Rules
  163. [source, css]
  164. ----
  165. .v-datefield {}
  166. .v-datefield-calendarpanel {}
  167. .v-datefield-calendarpanel-header {}
  168. .v-datefield-calendarpanel-prevyear {}
  169. .v-datefield-calendarpanel-prevmonth {}
  170. .v-datefield-calendarpanel-month {}
  171. .v-datefield-calendarpanel-nextmonth {}
  172. .v-datefield-calendarpanel-nextyear {}
  173. .v-datefield-calendarpanel-body {}
  174. .v-datefield-calendarpanel-weekdays,
  175. .v-datefield-calendarpanel-weeknumbers {}
  176. .v-first {}
  177. .v-last {}
  178. .v-datefield-calendarpanel-weeknumber {}
  179. .v-datefield-calendarpanel-day {}
  180. ----
  181. The top-level element has the [literal]#++v-datefield++# style. In addition, the
  182. top-level element has a style name that indicates the resolution of the
  183. calendar, with [literal]#++v-datefield-++# basename and an extension, which is
  184. one of [literal]#++full++#, [literal]#++day++#, [literal]#++month++#, or
  185. [literal]#++year++#. The [literal]#++-full++# style is enabled when the
  186. resolution is smaller than a day.
  187. The [literal]#++v-datefield-calendarpanel-weeknumbers++# and
  188. [literal]#++v-datefield-calendarpanel-weeknumber++# styles are enabled when the
  189. week numbers are enabled. The former controls the appearance of the weekday
  190. header and the latter the actual week numbers.
  191. The other style names should be self-explanatory. For weekdays, the
  192. [literal]#++v-first++# and [literal]#++v-last++# styles allow making rounded
  193. endings for the weekday bar.
  194. [[components.datefield.resolution]]
  195. == Date Resolution
  196. In addition to display a calendar with dates, [classname]#DateField# can also
  197. display just the month or year. The visibility of the input components is
  198. controlled by [methodname]#setDateResolution()#, which you can set with [methodname]#setResolution()#.
  199. The method takes as its parameter the highest resolution date element that should
  200. be visible. Please see the API Reference for the complete list of resolution parameters.
  201. [[components.datefield.locale]]
  202. == DateField Locale
  203. The date is displayed according to the locale of the user, as reported
  204. by the browser. You can set a custom locale with the [methodname]#setLocale()#
  205. method of [classname]#AbstractComponent#, as described in
  206. <<dummy/../../../framework/components/components-features#components.features.locale,"Locale">>.
  207. Only Gregorian calendar is supported.
  208. [[components.datefield.weeknumbers]]
  209. == Week Numbers
  210. You can enable week numbers in a date field with
  211. [methodname]#setShowISOWeekNumbers()#. The numbers are shown according to the ISO 8601 standard in a column on the left side of the field.