Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AEM/CQ: Checkbox is checked saves a Boolean value of TRUE,How to save a Boolean value as FALSE if we Unchecked?

For Example,I have created a check box 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 check box it is showing the property "Sample" like below sample Boolean true (working fine) If we Unchecked/disable the checkbox then it is not showing the property "Sample"

Expectation: I want to show Sample Boolean false if we Unchecked/disable the checkbox

like image 718
User5053 Avatar asked Feb 04 '23 08:02

User5053


1 Answers

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

One such suffix for example is the @UseDefaultWhenMissing suffix, which should be exactly what you are looking for.

From the documentation:

As described above, @DefaultValue only takes effect if no value is provided for a particular parameter. However, in some cases, such as HTML checkboxes, this isn't sufficient because the parameter isn't submitted at all. To handle this scenario, you can use the @UseDefaultWhenMissing suffixed parameter.

<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 have to do in your dialog definition is to 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 close attention to the names of the fields:

./sample@DefaultValue and ./sample@UseDefaultWhenMissing.

It 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 you can read about in the Sling documentation:

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

like image 73
Jens Avatar answered Apr 26 '23 22:04

Jens