Getting Default Checked Checkbox in CQ5
Asked Answered
R

3

18

I am trying to have a default checked checkbox on a component dialog when editing. Here are the properties on the field:

jcr:primaryType: widget
checked: true (boolean) *Documentation says this determines default checked status
type: checkbox (string) *read this as a fix to making checkbox selections stick
xtype: selection (string)
name: ./foo (string)
fieldValue: true (string)
Rozek answered 7/5, 2012 at 14:7 Comment(0)
F
19

Yes, it looks like the documentation is a little wonky. I did some experimenting, and this combination of properties works for me:

defaultValue (String) true
fieldLabel (String) Foo Mode
inputValue (String) false
jcr:primaryType (Name) cq:Widget
name (String) ./foomode
type (String) checkbox
xtype (String) selection

The defaultValue property appears to be the key.

You do have cq:Widget for your primary type, not widget, do you not?

Floriated answered 8/5, 2012 at 14:44 Comment(3)
Thank you so much. I would not have devised this combination on my own. C'mon, Adobe, manage your documentation better!Rozek
I've faced the exact same problem and made worse by the imprecise CQ documentation. Looking deeper into this, the above combination will successfully render a "checked" checkbox on the Dialog but that will not result in setting the underlying JCR property representing this checkbox element, i.e. there is no ./foomode property pre-created for this checkbox, it will only get created after the user access the dialog and hit the "OK" button even when not making any changes.Nakamura
Node properties aren't created until a POST request creates them. Also, remember a checkbox isn't submitted unless it is checked. This isn't a function of CQ, it's in the HTML specifications and implemented by the browser. If you want to create the node property regardless of whether the checkbox is checked or not, use the SlingPostServlet's @UseDefaultWhenMissing suffix. See the Apache Sling docs: sling.apache.org/documentation/bundles/….Sprague
F
5

To have this saved as a Boolean...

<nodeName
jcr:primaryType="cq:Widget" 
fieldLabel="check this nodename" 
name="./nodeName" 
defaultValue="{Boolean}false" 
type="checkbox"
xtype="selection" />

<nodeNameHint
  jcr:primaryType="cq:Widget"
  ignoreData="{Boolean}true"
  name="./nodeName@TypeHint"
  value="Boolean"
  xtype="hidden"/>
Freddie answered 16/7, 2012 at 18:27 Comment(0)
S
5

To set checkbox with a default value of checked and save the property as a Boolean property type in the JCR (rather than a String), use the following Classic UI settings:

<myCheckbox
    jcr:primaryType="cq:Widget"
    fieldLabel="My Checkbox"
    name="./myCheckbox"
    value="true"
    defaultValue="true"
    checkboxBoolTypeHint="{Boolean}true"
    type="checkbox"
    xtype="selection"/>

Or use the following settings in the Granite Touch UI:

<myCheckbox
    jcr:primaryType="nt:unstructured"
    sling:resourceType="granite/ui/components/foundation/form/checkbox"
    text="My Checkbox"
    name="./myCheckbox"
    value="true"
    checked="true"/>
<myCheckboxType
    jcr:primaryType="nt:unstructured"
    sling:resourceType="granite/ui/components/foundation/form/hidden"
    name="./myCheckbox@TypeHint"
    value="Boolean"/>

There's a detailed writeup and demo at http://www.nateyolles.com/blog/2015/11/aem-checkboxes-using-sling-post-servlet.

Sprague answered 18/3, 2016 at 5:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.