Datepicker to save value in YYYY/MM/DD format

ashishkhadpe

11-09-2020

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"/>

Accepted Solutions (1)

Accepted Solutions (1)

Suraj_Kamdi

MVP

13-09-2020

Answers (2)

Answers (2)

Theo_Pendle

MVP

11-09-2020

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! 🙂

 

Vijayalakshmi_S

MVP

11-09-2020

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: