Default value for Date field? | Community
Skip to main content
Level 4
February 5, 2023

Default value for Date field?

  • February 5, 2023
  • 2 replies
  • 2264 views

I have a Date field I've added to all pages in my site, called 'pageDate'.

 

<pageDate jcr:primaryType="nt:unstructured" sling:resourceType="granite/ui/components/coral/foundation/form/datepicker" displayedFormat="MMMM DD, YYYY" displayTimezoneMessage="{Boolean}true" fieldDescription="Page Date used for Sorting" fieldLabel="Page Date used for sorting purposes in search scenarios" name="./pageDate" renderReadOnly="{Boolean}false" required="{Boolean}true" type="date" />

 

How can I make this field default to the current date upon page creation?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

2 replies

Anmol_Bhardwaj
Community Advisor
Community Advisor
February 6, 2023

To add a default value to a field, you can use "defaultValue"

<pageDate jcr:primaryType="nt:unstructured" sling:resourceType="granite/ui/components/coral/foundation/form/datepicker" displayedFormat="MMMM DD, YYYY" displayTimezoneMessage="{Boolean}true" fieldDescription="Page Date used for Sorting" fieldLabel="Page Date used for sorting purposes in search scenarios" name="./pageDate" renderReadOnly="{Boolean}false" required="{Boolean}true" type="date" defaultValue="{Date}{now}" />
Level 4
February 6, 2023

This leads to a build error:

 

[ERROR] ValidationViolation: "jackrabbit-nodetypes: Property {}defaultValue does not have a valid value: not a valid date format: {now}"

Anmol_Bhardwaj
Community Advisor
Community Advisor
February 7, 2023

{Date} is the data type and then in place of {now}, you would need to set time dynamically from the back-end.

Something like this,

Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT")); child.setProperty(cpropertyName,calendar);

 

You can refer this question for detailed explanations:
experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/setting-jcr-property-of-type-date-with-value-dynamically/m-p/323282 

Manu_Mathew_
Community Advisor
Community Advisor
February 6, 2023

@dylanmccurry You can use the _cq_template/.content.xml node for this, with the following contents:

<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:cq="http://www.day.com/jcr/cq/1.0"
xmlns:jcr="http://www.jcp.org/jcr/1.0"
xmlns:nt="http://www.jcp.org/jcr/nt/1.0"
jcr:primaryType="nt:unstructured"
pageDate="{Date}2023-02-06T00:00:00.000+05:30">
</jcr:root>

Hope this helps!

Level 4
February 6, 2023

Yes, but what about tomorrow?  How do I make the system automatically select the current date?

Manu_Mathew_
Community Advisor
Community Advisor
February 7, 2023

@dylanmccurry Probably for the dynamic value you might need to inject it via dialog clientlib js, if {now} is not working or via sling model.