Checkbox value is preserved
I have 3 checkbox widgets in my dialog and based on no selection code do the logic, the problem occurs when I check option 1 of option2, after which they set their value in the content node to "ON". But if I edit the same component 1, select option 2, I uncheck it and click OK, "Still in content node" exists with value "ON".
<?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"
jcr:primaryType="cq:Dialog"
title="dialog"
xtype="dialog">
<items
jcr:primaryType="cq:Widget"
xtype="tabpanel">
<items jcr:primaryType="cq:WidgetCollection">
<tab1
jcr:primaryType="cq:Panel"
title="Select Options">
<items jcr:primaryType="cq:WidgetCollection">
<facebook
jcr:primaryType="cq:Widget"
fieldLabel="Facebook"
name="./facebook"
xtype="checkbox"/>
<twitter
jcr:primaryType="cq:Widget"
fieldLabel="Twitter"
name="./twitter"
xtype="checkbox"/>
<linkedin
jcr:primaryType="cq:Widget"
fieldLabel="Linkedin"
name="./linkedin"
xtype="checkbox"/>
</items>
</tab1>
</items>
</items>
</jcr:root>
source to share
I haven't had much success with the type checkbox
. Rather, try selection
xtype with type checkbox
.
<facebook
jcr:primaryType="cq:Widget"
fieldLabel="Facebook"
name="./facebook"
type="checkbox"
xtype="selection"/>
source to share
the problem is that if you uncheck the box, the form just doesn't POST that field, so Sling just doesn't modify this property at all.
To be able to preserve unchecked state, you need to add default values. You can do this with Sling Suffixes .
-
parameter@Delete
, causes the property to be removed from jcr if no other value was submitted. in your case you cannot interpret the property as unchecked -
parameter@DefaultValue
can be used to send a default value in case of sending a parameter without any value -
parameter@UseDefaultWhenMissing = true
can be used to force Sling to use the default when no parameter was sent at all.
So, in your specific case, add hidden fields ( xtype:hidden
) with the following names and values.
-
./facebook@DefaultValue="off"
-
./facebook@UseDefaultWhenMissing="true"
This will mean that the "off" value is used when the checkbox is unchecked.
ps-Yes, it's annoying using checkboxes in AEM / CQ
source to share