If I am making the checkbox default checked, on some existing pages checkbox is not showing checked | Community
Skip to main content
New Member
July 25, 2025
Solved

If I am making the checkbox default checked, on some existing pages checkbox is not showing checked

  • July 25, 2025
  • 1 reply
  • 314 views

If I am making the checkbox default checked using coral2 checkbox field,

 

<enableBanner
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/foundation/form/checkbox"
fieldDescription="Check if shorter banner is required."
listener="toggle.checkboxShorterBanner"
name="./enableBanner"
text="Shorter Banner"
uncheckedValue="false"
value="true"
defaultChecked="{Boolean}true"/>

and in SlingModel using it

@ValueMapValue
@Default(booleanValues = true)
private boolean enableBanner;

Sightly code
<div id="${properties.isBanner ? 'main_banner':''}"
class="${bannerPowerModel.enableBanner? shortHeroClass : ''}">
I want to add shortHeroClass if it is checked otherwise no


I can see the difference in html where it is coming as checked by default
<div class="coral-Form-fieldwrapper coral-Form-fieldwrapper--singleline"><label class="coral-Checkbox coral-Form-field">
<input type="checkbox" name="./shorterBanner" value="true" data-validation="" checked="" class="coral-Checkbox-input" data-listener="toggle.checkboxShorterBanner">

where it is not coming as checked by default, checked="" is not coming in html
<div class="coral-Form-fieldwrapper coral-Form-fieldwrapper--singleline"><label class="coral-Checkbox coral-Form-field">
<input type="checkbox" name="./enableBanner" value="true" data-validation="" class="coral-Checkbox-input" data-listener="toggle.checkboxShorterBanner">
But on this page if I am dragging dropping this component, then it is showing checkbox checked by default
I also want to show hide tab based on checkbox selection
 
 
 
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by SantoshSai

Hi @vineetgupta,

  • The defaultChecked="{Boolean}true" attribute only sets the initial value when no value is stored in the JCR.

  • If an existing page already has the property enableBanner (even as false or empty), the dialog will render based on the stored value, not the defaultChecked value.

Here is what coming in my mind:

1. Fix existing content nodes

To force all existing pages to have enableBanner=true (checked by default), you can run a JCR content update (via Groovy script, ACS Commons Bulk Editor, or a query).
Example Groovy script:

def rootPath = "/content/your-site"
def nodes = getNode(rootPath).recurse().findAll { it.hasNode("enableBanner") == false }
nodes.each {
    it.setProperty("enableBanner", true)
}

This ensures that older pages have enableBanner set correctly.

2. Use @Default(booleanValues = true) in SlingModel

You are already using:

@ValueMapValue
@Default(booleanValues = true)
private boolean enableBanner;

This is good - it ensures that if the property is missing in JCR, the model returns true.
However, in Sightly/HTL, you should reference bannerPowerModel.enableBanner instead of properties.isBanner to ensure the model's logic takes precedence.

 

1 reply

SantoshSai
Community Advisor
SantoshSaiCommunity AdvisorAccepted solution
Community Advisor
July 25, 2025

Hi @vineetgupta,

  • The defaultChecked="{Boolean}true" attribute only sets the initial value when no value is stored in the JCR.

  • If an existing page already has the property enableBanner (even as false or empty), the dialog will render based on the stored value, not the defaultChecked value.

Here is what coming in my mind:

1. Fix existing content nodes

To force all existing pages to have enableBanner=true (checked by default), you can run a JCR content update (via Groovy script, ACS Commons Bulk Editor, or a query).
Example Groovy script:

def rootPath = "/content/your-site"
def nodes = getNode(rootPath).recurse().findAll { it.hasNode("enableBanner") == false }
nodes.each {
    it.setProperty("enableBanner", true)
}

This ensures that older pages have enableBanner set correctly.

2. Use @Default(booleanValues = true) in SlingModel

You are already using:

@ValueMapValue
@Default(booleanValues = true)
private boolean enableBanner;

This is good - it ensures that if the property is missing in JCR, the model returns true.
However, in Sightly/HTL, you should reference bannerPowerModel.enableBanner instead of properties.isBanner to ensure the model's logic takes precedence.

 

Santosh Sai