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)
justacoder
  • 2,684
  • 6
  • 47
  • 78

3 Answers3

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?

David Gorsline
  • 4,933
  • 12
  • 31
  • 36
  • 2
    Thank you so much. I would not have devised this combination on my own. C'mon, Adobe, manage your documentation better! – justacoder May 08 '12 at 15:13
  • 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. – HackerMonkey Jun 16 '12 at 00:36
  • 1
    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: https://sling.apache.org/documentation/bundles/manipulating-content-the-slingpostservlet-servlets-post.html#usedefaultwhenmissing. – nateyolles Mar 18 '16 at 05:14
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"/>
Raj Kumar Samala
  • 561
  • 12
  • 31
4

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.

nateyolles
  • 1,851
  • 5
  • 17
  • 23