[MGNLUI-4271] DateField: default value can not be set -> yaml parses the Date (and Time) String automatically Created: 18/Aug/17  Updated: 13/Sep/17  Resolved: 13/Sep/17

Status: Closed
Project: Magnolia UI
Component/s: forms
Affects Version/s: 5.5.5
Fix Version/s: None

Type: Bug Priority: Neutral
Reporter: Christian Ringele Assignee: Unassigned
Resolution: Not an issue Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Relates
relates to MGNLUI-4270 DateField: Default value is not picke... Closed
Template:
Acceptance criteria:
Empty
Task DoD:
[ ]* Doc/release notes changes? Comment present?
[ ]* Downstream builds green?
[ ]* Solution information and context easily available?
[ ]* Tests
[ ]* FixVersion filled and not yet released
[ ]  Architecture Decision Record (ADR)
Bug DoR:
[ ]* Steps to reproduce, expected, and actual results filled
[ ]* Affected version filled
Date of First Response:

 Description   

Configuring on a DateFieldDefintion a default value like this defaultValue: 2015-01-01 throws an exception:

Caused by: java.text.ParseException: Unparseable date: "Thu Jan 01 01:00:00 CET 2015"
	at java.text.DateFormat.parse(DateFormat.java:366)
	at info.magnolia.ui.vaadin.integration.jcr.DefaultPropertyUtil.createTypedValue(DefaultPropertyUtil.java:164)
	... 137 more

The reason:
Yaml seems to automatically parse String representations of Date (and of Time).
Regarding Date:
Adding a Date value as String to the yaml defaultValue: 2015-01-01 and reading the String directly from the defintion it returns this Thu Jan 01 01:00:00 CET 2015.
When its not matching the Date formate it returns the String 1:1, for example: Adding this value to the yaml defaultValue: 2015.01.01 and reading the String directly from the defintion it returns this {2015.01.01}}.

Regarding Time:
Adding a Time value as String to the yaml example: 11:00 and reading the String directly from the defintion it returns this 660 (which is a wrong int representation!).
When its not matching the Time formate it returns the String 1:1, for example: Adding this value to the yaml example: 11.00 and reading the String directly from the defintion it returns this {11.00}}.



 Comments   
Comment by Antti Hietala [ 13/Sep/17 ]

Default value can be set but you must enclose it in quotes when using YAML:

defaultValue: "2017-10-31"

If you define the dateFormat or timeFormat properties then provide the defaultValue using those same formats.

dateFormat: yyyy-MM-dd
timeFormat: h:mm a
defaultValue: "2017-10-31 3:09 PM"

The format properties are used in two ways:

  1. To display dates to users in a familiar format such as MM/dd/yyyy in the US.
  2. To parse the default value.

Case 2 is a bit flaky but mostly it works fine. The system expects the default value to be given as dateFormat + " " + timeFormat, see DateFieldFactory (Git). This works reliably for simple formats like yyyy-MM-dd HH:mm but if you define a very exotic format such as EEE, MMM d, yyyy (Tue, Oct 31, 2017) the system will fail to parse the default value correctly.

Date field documentation was updated.

Comment by Christian Ringele [ 13/Sep/17 ]

ahietala ah man I haven't realised that it needs to be provided as a Sting... my bad.

Generated at Mon Feb 12 09:15:02 CET 2024 using Jira 9.4.2#940002-sha1:46d1a51de284217efdcb32434eab47a99af2938b.