Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.

Default value for Date field?

Avatar

Level 5

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?

7 Replies

Avatar

Community Advisor

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

Avatar

Level 5

This leads to a build error:

 

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

Avatar

Community Advisor

{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 

Avatar

Level 5

So this works if I put it into the getPageDate() property accessor, but the dialog itself is not updated with the current date after re-opening, it's still blank.  Is there a way to intercept some sort of OnSave where I can add logic like this?  e.g., I want the content author to visually see the value that we are defaulting to?

Avatar

Community Advisor

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

Avatar

Level 5

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

Avatar

Community Advisor

@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.