i am trying to extend checkbox and add a descreption and value to each item | Adobe Higher Education
Skip to main content
Level 2
September 7, 2023
Répondu

i am trying to extend checkbox and add a descreption and value to each item

the thing is I've tried to connect the java with the dialog but its not working 
what i did wrong

html 

<div data-sly-use.valueAndDescription="com.adobe.aem.guides.wknd.core.models.ValuesAndDescriptionsModel">
<input type="checkbox">
<h2>${valueAndDescription.fieldLabel}</h2>
<p>${valueAndDescription.fieldDescription}</p>


<ul data-sly-list="${valueAndDescription.valuesAndDescriptions}">
<li>
<p>Value:</p> ${item.value}<br>
<p>Description:</p> ${item.description}
</li>
</ul>
</div>

content.xml

<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
cq:icon="text"
jcr:primaryType="cq:Component"
jcr:title="Adaptive Form CheckBox"
sling:resourceSuperType="/libs/fd/af/components/guidecheckbox"
componentGroup="Adaptive Form"/>

 dialog 

<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0"
xmlns:granite="http://www.adobe.com/jcr/granite/1.0"
xmlns:cq="http://www.day.com/jcr/cq/1.0"
xmlns:jcr="http://www.jcp.org/jcr/1.0"
xmlns:nt="http://www.jcp.org/jcr/nt/1.0"
jcr:primaryType="nt:unstructured"
jcr:title="Adaptive Form CheckBox field"
sling:resourceType="cq/gui/components/authoring/dialog"

>
<content
granite:class="cmp-adaptiveform-checkbox__editdialog"
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/container">
<items jcr:primaryType="nt:unstructured">
<valuesAndDescriptions
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/form/multifield"
fieldLabel="Values and Descriptions"
name="./valuesAndDescriptions"
fieldDescription="Add values and their descriptions for the checkbox options.">
<field
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/container">
<items jcr:primaryType="nt:unstructured">
<value
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/form/textfield"
fieldLabel="Value"
name="value"
required="{Boolean}true"/>
<description
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/form/textfield"
fieldLabel="Description"
name="description"
fieldDescription="Add a description for the value."
/>
</items>
</field>
</valuesAndDescriptions>
</items>
</content>
</jcr:root>

Interface 

package com.adobe.aem.guides.wknd.core.models;

import com.adobe.aem.guides.wknd.core.models.impl.ValueAndDescriptionImpl;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.SlingObject;

import java.util.List;
import java.util.Map;

public interface ValuesAndDescriptionsModel {

public String getFieldLabel();
public String getName();
public String getFieldDescription();
public String getValue();

abstract List<Map<String, String>> getValuesAndDescriptions();
}

java 

package com.adobe.aem.guides.wknd.core.models.impl;

import com.adobe.aem.guides.wknd.core.models.ValuesAndDescriptionsModel;
import com.adobe.cq.wcm.core.components.util.AbstractComponentImpl;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.SlingObject;
import org.apache.sling.models.annotations.injectorspecific.ValueMapValue;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Model(adaptables = SlingHttpServletRequest.class,
adapters = {ValuesAndDescriptionsModel.class},
defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL,
resourceType=ValueAndDescriptionImpl.RESOURCE_TYPE)
public class ValueAndDescriptionImpl extends AbstractComponentImpl implements ValuesAndDescriptionsModel{

@SlingObject

private Resource resource;

@ValueMapValue
private String[] valuesAndDescriptions;

@ValueMapValue
private String value;

public static final String RESOURCE_TYPE = "dow-platform/components/forms/checkbox";


@Override
public String getValue() {
return this.value;
}

@Override
public String getFieldLabel() {
return resource.getValueMap().get("fieldLabel", String.class);
}

@Override
public String getName() {
return resource.getValueMap().get("name", String.class);
}

@Override
public String getFieldDescription() {
return resource.getValueMap().get("fieldDescription", String.class);
}



@Override
public List<Map<String, String>> getValuesAndDescriptions() {
List<Map<String, String>> result = new ArrayList<>();

for (String valueAndDescription : valuesAndDescriptions) {
String[] parts = valueAndDescription.split("\\|");
if (parts.length == 2) {
Map<String, String> entry = new HashMap<>();
entry.put("value", parts[0]);
entry.put("description", parts[1]);
result.add(entry);
}
}

return result;
}
}

 

Ce sujet a été fermé aux réponses.
Meilleure réponse par AsifChowdhury

so if i make it  like this 

          xmlns:granite="http://www.adobe.com/jcr/granite/1.0"
          xmlns:cq="http://www.day.com/jcr/cq/1.0"
          xmlns:jcr="http://www.jcp.org/jcr/1.0"
          xmlns:nt="http://www.jcp.org/jcr/nt/1.0"
          jcr:primaryType="nt:unstructured"
          jcr:title="Adaptive Form CheckBox field"
          sling:resourceType="cq/gui/components/authoring/dialog"

>
    <content
            granite:class="cmp-adaptiveform-checkbox__editdialog"
            jcr:primaryType="nt:unstructured"
            sling:resourceType="granite/ui/components/coral/foundation/container">

                        <value
                                jcr:primaryType="nt:unstructured"
                                sling:resourceType="granite/ui/components/coral/foundation/form/textfield"
                                fieldLabel="Value"
                                name="./value"
                                required="{Boolean}true"/>
                        <description
                                jcr:primaryType="nt:unstructured"
                                sling:resourceType="granite/ui/components/coral/foundation/form/textfield"
                                fieldLabel="Description"
                                name="./description"
                                fieldDescription="Add a description for the value."
                        />
                    </items>

        </items>
    </content>
</jcr:root>

it should work like getting each item on the valuemapvalue

@mohammedskouti 

You can't directly remove the multifield. You need to implement the business logic. If you need multiple item for value and description then you can use multifield but you have to find out the way of retrieving the value from a multifield. If you need only one value and description for each component then you can remove the multifield. And can get the value from resource value map by,

@ValueMapValue
private String value;

  Make sure the value is coming from the resource.

3 commentaires

AsifChowdhury
Community Advisor
Community Advisor
September 7, 2023

Hi @mohammedskouti 

The problem is in the dialog xml.

 

<value jcr:primaryType="nt:unstructured" sling:resourceType="granite/ui/components/coral/foundation/form/textfield" fieldLabel="Value" name="value" required="{Boolean}true"/> <description jcr:primaryType="nt:unstructured" sling:resourceType="granite/ui/components/coral/foundation/form/textfield" fieldLabel="Description" name="description" fieldDescription="Add a description for the value." />

 

In the name, it should be like name="./value" and name="./description".

As like you have written for valuesAndDescription,

name="./valuesAndDescriptions"

Try with this. Hope it will resolve.

Level 2
September 7, 2023

still not working 

AsifChowdhury
Community Advisor
Community Advisor
September 7, 2023

@mohammedskouti 

How you sure a multifield is going to be stored like an array in CRX?

@ValueMapValue
private String[] valuesAndDescriptions; 
<valuesAndDescriptions jcr:primaryType="nt:unstructured" sling:resourceType="granite/ui/components/coral/foundation/form/multifield" fieldLabel="Values and Descriptions" name="./valuesAndDescriptions" fieldDescription="Add values and their descriptions for the checkbox options.">

You have given the resource type is a multifield.

As far as I know multifield are stored in CRX like node. It create a new node each time we add (called item0, item1).

By using @ValueMapValue annotation you are trying to get values from resource Value Map. So check by debugging your code that what is this returning and try to map accordingly.

kautuk_sahni
Community Manager
Community Manager
September 12, 2023

@mohammedskouti Do you find the suggestions from Asif useful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. 

Kautuk Sahni
Level 3
September 12, 2023

Nothing solve the problems

I cant use the functionality of the form core component on the extended one

kautuk_sahni
Community Manager
Community Manager
September 13, 2023

Moved this thread to AEM Forms Community. 

@pulkit_jain_ @vijay_katoch @workflowuser, can you review this Forms Question and share your thoughts? 

Kautuk Sahni