justacoder
justacoder

Reputation: 2704

Getting Default Checked Checkbox in CQ5

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)

Upvotes: 18

Views: 25682

Answers (3)

michael hewson
michael hewson

Reputation: 51

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"/>

Upvotes: 5

nateyolles
nateyolles

Reputation: 1921

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.

Upvotes: 5

David Gorsline
David Gorsline

Reputation: 5018

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?

Upvotes: 19

Related Questions