granite/ui/components/coral/foundation/form/checkbox uncheckedValue not working | Community
Skip to main content
Prashardan
New Participant
April 14, 2025
Solved

granite/ui/components/coral/foundation/form/checkbox uncheckedValue not working

  • April 14, 2025
  • 3 replies
  • 961 views

Hi Team

 

I have an issue when checkbox is unchecked the value is empty "" instead I would like to store it as false. 
When it is checked I am getting as "true"

 

could you please advise me on the below 2 issues:

1. When checkbox is not checked or unchecked I want value as false

2. When checkbox is checked, i am getting "true" in json which is a string. Can I get as boolean like true.

 

below is my node structure :

<disable
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/form/checkbox"
fieldDescription="Checking will add a check icon"
name="./disable"
text="disable"
checked="{Boolean}false"
value="{Boolean}true"
deleteHint="{Boolean}false"/>

Best answer by SantoshSai

Hi @prashardan,

1. When checkbox is unchecked, store value as false

By default, unchecked checkboxes don't submit any value — which is why you’re seeing "" (empty string) or nothing in the JCR node.

Try this:

<disable
    jcr:primaryType="nt:unstructured"
    sling:resourceType="granite/ui/components/coral/foundation/form/checkbox"
    fieldDescription="Checking will add a check icon"
    name="./disable"
    text="disable"
    checked="{Boolean}false"
    uncheckedValue="{Boolean}false"
    value="{Boolean}true"
    deleteHint="{Boolean}true"/>

Key changes:

  • uncheckedValue: This ensures false is stored when the checkbox is not checked.

  • deleteHint=true: Ensures the field gets deleted or replaced with the uncheckedValue during POST.

2. Getting "true" as string, want actual boolean true

When reading the value in your servlet/model or exporting it to JSON via Sling Model, sometimes "true" appears as a string because of how it's stored or serialized.

Fixes:

  1. Make sure you're using the correct data type in your Java model.

@ValueMapValue
@Optional
private boolean disable;

This will automatically treat "true" or "false" as a boolean, not a string.

  1. If using Sling Model Exporter, ensure the value is exported as a boolean:

@Exporter(name = "jackson", extensions = "json")
public class MyModel implements MyModelInterface {

    @ValueMapValue
    private boolean disable;

    @Override
    public boolean isDisable() {
        return disable;
    }
}

This will ensure your final JSON has:

"disable": true

or

"disable": false

— as actual booleans, not strings.

 

Hope that helps!
Regards,
Santosh

3 replies

SantoshSai
SantoshSaiAccepted solution
New Participant
April 16, 2025

Hi @prashardan,

1. When checkbox is unchecked, store value as false

By default, unchecked checkboxes don't submit any value — which is why you’re seeing "" (empty string) or nothing in the JCR node.

Try this:

<disable
    jcr:primaryType="nt:unstructured"
    sling:resourceType="granite/ui/components/coral/foundation/form/checkbox"
    fieldDescription="Checking will add a check icon"
    name="./disable"
    text="disable"
    checked="{Boolean}false"
    uncheckedValue="{Boolean}false"
    value="{Boolean}true"
    deleteHint="{Boolean}true"/>

Key changes:

  • uncheckedValue: This ensures false is stored when the checkbox is not checked.

  • deleteHint=true: Ensures the field gets deleted or replaced with the uncheckedValue during POST.

2. Getting "true" as string, want actual boolean true

When reading the value in your servlet/model or exporting it to JSON via Sling Model, sometimes "true" appears as a string because of how it's stored or serialized.

Fixes:

  1. Make sure you're using the correct data type in your Java model.

@ValueMapValue
@Optional
private boolean disable;

This will automatically treat "true" or "false" as a boolean, not a string.

  1. If using Sling Model Exporter, ensure the value is exported as a boolean:

@Exporter(name = "jackson", extensions = "json")
public class MyModel implements MyModelInterface {

    @ValueMapValue
    private boolean disable;

    @Override
    public boolean isDisable() {
        return disable;
    }
}

This will ensure your final JSON has:

"disable": true

or

"disable": false

— as actual booleans, not strings.

 

Hope that helps!
Regards,
Santosh

Santosh Sai
DPrakashRaj
New Participant
April 14, 2025
Prashardan
New Participant
April 15, 2025

@dprakashraj I have tried the same way but it is not working. I am using this in the content fragment model. Checked value is coming as true which is fine but unCheckedValue is not coming as false

arunpatidar
New Participant
April 14, 2025

Hi @prashardan 

Can you try with new page? it may not work with existing authored component.

Arun Patidar
Prashardan
New Participant
April 15, 2025

I am using this in the content fragment model. Checked value is coming as true which is fine but unCheckedValue is not coming as false