Expand my Community achievements bar.

SOLVED

Datepicker to save value in YYYY/MM/DD format

Avatar

Level 4

Hi All,

 

We have just migrated from classic UI to touch UI and we are currently using a datepicker field.

 

The problem we are facing right now is that, our classic UI dialog have stored the value for date in YYYY/MM/DD format and we are trying to save in same format in touch UI using granite/ui/components/coral/foundation/form/datepicker. I have added below properties to the field but still not working. Any quick solution?

 

<preselectedDate
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/form/datepicker"
fieldDescription="Enter date in this format yyyy/mm/dd"
fieldLabel="Pre-selected date"
name="./preselectedDate"
required="{Boolean}false"
displayedFormat="YYYY/MM/DD"
valueFormat="YYYY/MM/DD"
storedValue="YYYY/MM/DD"
type="date"/>
1 Accepted Solution

Avatar

Correct answer by
Community Advisor
7 Replies

Avatar

Community Advisor

Hi @ashishkhadpe,

For the datepicker field,

  • valueFormat  should be YYYY-MM-DD[T]HH:mm:ss.SSSZ. Otherwise while submitting the dialog, we will get PerisistenceException (Invalid date)
  • displayedFormat can be with slash (like the one you have - YYYY/MM/DD)
  • storedValue is not a valid property and hence to be removed.

If you still want the date to be saved like YYYY/MM/DD, alternative it is to save it as String type and not as Date type. This is possible with property named typeHint and value as String. 

Retrieval logic should then be handled accordingly. 

Related doc for reference:

Avatar

Level 4
What are the changes required for multi-field. Any idea?

Avatar

Level 10

Hi @ashishkhadpe,

Is your problem the following?

Theo_Pendle_0-1599845356456.png

If this is the case then here is the answer

The reason for your problem is that providing a custom valueFormat, you are violating the JCR's definition of a date, which you can see here in the spec: https://docs.adobe.com/docs/en/spec/jcr/1.0/6.2.5.1_Date.html 

As you can see, a JCR date must follow a certain format, which is not the case for your property. Instead, you should save your date as a String by adding 

typeHint="String"

The type hint (documentation here: https://sling.apache.org/documentation/bundles/manipulating-content-the-slingpostservlet-servlets-po...) tells Sling (which is receiving the POST request to update the JCR when you save a component) to override the default property format and save the value as a "2020/09/11" instead of a Date.

Hope that helps, let me know!

 

Avatar

Correct answer by
Community Advisor

Avatar

Level 4
I guess the solution given in above blog works fine for single field. What are the changes required for multi-field. Any idea?