AEM / CQ: checkbox checked, keeps boolean TRUE, how to store boolean as FALSE if we unchecked the checkbox?

For example, I created a checkbox with below properties

<checkbox1
                    jcr:primaryType="cq:Widget"
                    checked="false"
                    defaultValue="false"
                    fieldLabel="Sample"
                    inputValue="true"
                    name="./sample"
                    checkboxBoolTypeHint="{Boolean}true"
                    type="checkbox"
                    xtype="selection">
                    <listeners
                        jcr:primaryType="nt:unstructured"
                        check="function(isChecked){var panel = this.findParentByType('panel'); var fields = panel.find('name', './sample'); for (var i=0;i&lt;fields.length; i++) {if (fields[i].xtype == 'hidden') { if (isChecked.checked) {fields[i].setDisabled(true);} else {fields[i].setDisabled(false);}}}}"/>

</checkbox1>
<hiddenCheckbox1
                    jcr:primaryType="cq:Widget"
                    disabled="{Boolean}true"
                    ignoreData="{Boolean}true"
                    name="./sample"
                    value="{Boolean}false"
                    xtype="hidden"/>

      

If we checked / enabled the checkbox, it will display the "Example" property as below    sample Boolean true (works fine) If the checkbox is unchecked or disabled, it does not display the "Example" property

Expectation: I want to show Sample Boolean false if we unchecked the checkbox

+3


source to share


1 answer


You might want to check the Sling POST servlet documentation. This servlet is called when your dialog is posted. It has something called Suffixes that you can use in your dialog box to give the POST servlet some hints about what your fields are doing.

One such suffix is ​​the suffix @UseDefaultWhenMissing

, which should be exactly what you are looking for.

From the documentation:

As described above, @DefaultValue takes effect if no value is set for a specific parameter. However, in some cases, such as HTML checkboxes, this is not sufficient since the parameter is not sent at all. You can use the @UseDefaultWhenMissing parameter to handle this scenario.

<form method="POST" action="/content/page/first" enctype="multipart/form-data">
    <input name="queryIgnoreNoise" class="input" type="checkbox" value="true"/>
    <input type="hidden" name="queryIgnoreNoise@DefaultValue" value="false"/> 
    <input type="hidden" name="queryIgnoreNoise@UseDefaultWhenMissing" value="true"/>
</form>

      

So what you need to do in your dialog definition is add two additional hidden fields:



<checkbox1DefaultValue
    jcr:primaryType="cq:Widget"
    name="./sample@DefaultValue"
    value="{Boolean}false"
    xtype="hidden"/>
<checkbox1UseDefaultWhenMissing
    jcr:primaryType="cq:Widget"
    name="./sample@UseDefaultWhenMissing"
    value="{Boolean}true"
    xtype="hidden"/>

      

Pay attention to the field names:

./sample@DefaultValue

and ./sample@UseDefaultWhenMissing

.

This is the name of the checkbox ( sample

) plus the name of the two required suffixes: @DefaultValue

and @UseDefaultWhenMissing

.

There are a few more nice suffixes in the Sling documentation:

https://sling.apache.org/documentation/bundles/manipulating-content-the-slingpostservlet-servlets-post.html

+6


source







All Articles