Multifield dropdown value is being treated as an invalid string in sling Modal | Community
Skip to main content
Abhishekty
Level 4
December 16, 2024
Solved

Multifield dropdown value is being treated as an invalid string in sling Modal

  • December 16, 2024
  • 1 reply
  • 571 views

Hi, 
I have created a AEM component with aem as clod services that contains a rich text editor in the dialog and a multifield dropdown. I have written a Sling model to handle the rich text formatting and logic to retrieve and use the selected dropdown value. However, I am facing an issue where the dropdown value is being processed incorrectly. The logs show warnings related to an invalid multifieldvaue option format, and the dropdown value retrieval is not getting as expected.

Here is my sling modal logic: 

@PostConstruct public void init() { Resource multifieldValueSelectionResource = componentResource.getChild("multifieldValueSelection"); if (multifieldValueSelectionResource != null) { logger.warn( "Found multifieldValueSelection resource at path: {}", multifieldValueSelectionResource.getPath()); multifieldValues = getLanguagesFromResource(multifieldValueSelectionResource); if (multifieldValues == null || multifieldValues.isEmpty()) { logger.warn("No multifieldValues found, defaulting to English."); multifieldValues.add("en"); } } else { logger.warn("multifieldValueSelection resource not found."); } } private List<String> getLanguagesFromResource(Resource multifieldValueSelectionResource) { List<String> multifieldValues = new ArrayList<>(); ValueMap properties; if (multifieldValueSelectionResource != null) { logger.warn( "Found multifieldValueSelection resource at path: {}", multifieldValueSelectionResource.getPath()); for (Resource child : multifieldValueSelectionResource.getChildren()) { logger.warn("Child Resource: {}", child.getPath()); ValueMap multifieldValueProps = child.adaptTo(ValueMap.class); // Adapt to ValueMap to fetch properties String multifieldValuesOption = multifieldValueProps.get("languageOptions", String.class); // Get multifieldValue option if (multifieldValuesOption != null) { logger.warn("Found multifieldValue option: {}", multifieldValuesOption); // If the multifieldValue format is valid, add it to the list String[] parts = multifieldValuesOption.split("="); // Assuming "multifieldValue=xyz" if (parts.length == 2) { String multifieldValue = parts[0].trim(); if (isValidMultifieldFormat(multifieldValue)) { multifieldValues.add(multifieldValue); } else { logger.warn("Invalid multifieldValue format detected: {}", multifieldValue); } } else { logger.warn("Invalid multifieldValue option format: {}", multifieldValuesOption); } } } logger.warn("Fetched multifieldValues from multifieldValueSelection resource: {}", multifieldValues); } // Default to "Python" if no valid multifieldValue is found if (multifieldValues.isEmpty()) { logger.warn("No valid multifieldValues found, defaulting to Python."); multifieldValues.add("python"); } return multifieldValues; } private boolean isValidMultifieldFormat(String multifieldValue) { logger.warn("call1-isValidMultifieldFormat for multifieldValue: {}", multifieldValue); List<String> validMultifieldValues = List.of("typescript", "java", "python", "javascript", "ruby", "css", "typescript"); if (multifieldValue == null || multifieldValue.trim().isEmpty()) { logger.warn( "Invalid multifieldValue format detected: {}. Allowed multifieldValues are: {}", multifieldValue, validMultifieldValues); return false; } if (!validMultifieldValues.contains(multifieldValue.trim().toLowerCase())) { logger.warn( "Invalid multifieldValue format detected: {}. Allowed multifieldValues are: {}", multifieldValue, validMultifieldValues); return false; } return true; } private String getCodeLanguage() { if (multifieldValues != null && !multifieldValues.isEmpty()) { String selectedMultifieldValue = multifieldValues.get(0).trim(); // First selected multifieldValue logger.warn("Fetched multifieldValue: {}", selectedMultifieldValue); if (!isValidMultifieldFormat(selectedMultifieldValue)) { logger.warn( "Invalid multifieldValue format detected: {}. Defaulting to Python.", selectedMultifieldValue); return "multifieldValue-tc"; // Default multifieldValue } logger.warn("Using selected multifieldValue: {}", selectedMultifieldValue); return "multifieldValue-" + selectedMultifieldValue .toLowerCase(); // Ensure it returns in lowercase, e.g., "multifieldValue-typescript" } logger.warn("No multifieldValue selected, defaulting to fc."); return "multifieldValue-fc"; // Default multifieldValue }


I have reviewed my XML configuration for the dialog, specifically the multifield dropdown section, in crxde the dropdown values are being captured and saved correctly. 

Here my multifiled  XML 

<multifieldValueSelection jcr:primaryType="nt:unstructured" sling:resourceType="granite/ui/components/coral/foundation/form/multifield" composite="{Boolean}true" fieldDescription="Select programming multifield value for sample" fieldLabel="Multifield values"> <field jcr:primaryType="nt:unstructured" sling:resourceType="granite/ui/components/coral/foundation/container" fieldLabel="multifield value Selection" name="./multifieldValueSelection"> <items jcr:primaryType="nt:unstructured"> <multifiledOption jcr:primaryType="nt:unstructured" sling:resourceType="granite/ui/components/foundation/form/select" fieldLabel="Select multifield value Type" name="./multifiledOption"> <items jcr:primaryType="nt:unstructured"> <option0 jcr:primaryType="nt:unstructured" text="Typescript" value="typescript"/> <option1 jcr:primaryType="nt:unstructured" text="CSS" value="css"/> <option2 jcr:primaryType="nt:unstructured" text="Python" value="python"/> </items> </multifiledOption> </items> </field> </multifieldValueSelection>


Can someone help me determine if I need to update my XML structure (dialog definition) or update my Sling model? I am unsure whether the issue lies in how the dropdown values are structured or how they are being processed in the Sling model.

Thanks 
Abhishek


This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by MukeshYadav_

Hi @abhishekty ,

I think you can directly use select with multiple="{Boolean}true" , this will allow to select multiple value from drop down

Sling model we can directly get as list

 

@ValueMapValue private List<String> languages; public List<String> getLanguages (){ return languages; }

 

 

 

<languages jcr:primaryType="nt:unstructured" sling:resourceType="granite/ui/components/coral/foundation/form/select" fieldLabel="Select languages" multiple="{Boolean}true" name="./languages"> <items jcr:primaryType="nt:unstructured"> <python jcr:primaryType="nt:unstructured" text="Python" value="python"/> <java jcr:primaryType="nt:unstructured" text="Java" value="java"/> <perl jcr:primaryType="nt:unstructured" text="Perl" value="perl"/> </items> </languages>

 

 

In sightly we can iterate

 

<ul data-sly-list.language="${model.languages}"> <li>${language}</li> </ul>

 

 

OR if you want as String array and comma separated then

@ValueMapValue private String[] languages; public String[] getLanguages (){ return languages; }${model.languages}

Thanks

1 reply

MukeshYadav_
Community Advisor
MukeshYadav_Community AdvisorAccepted solution
Community Advisor
December 17, 2024

Hi @abhishekty ,

I think you can directly use select with multiple="{Boolean}true" , this will allow to select multiple value from drop down

Sling model we can directly get as list

 

@ValueMapValue private List<String> languages; public List<String> getLanguages (){ return languages; }

 

 

 

<languages jcr:primaryType="nt:unstructured" sling:resourceType="granite/ui/components/coral/foundation/form/select" fieldLabel="Select languages" multiple="{Boolean}true" name="./languages"> <items jcr:primaryType="nt:unstructured"> <python jcr:primaryType="nt:unstructured" text="Python" value="python"/> <java jcr:primaryType="nt:unstructured" text="Java" value="java"/> <perl jcr:primaryType="nt:unstructured" text="Perl" value="perl"/> </items> </languages>

 

 

In sightly we can iterate

 

<ul data-sly-list.language="${model.languages}"> <li>${language}</li> </ul>

 

 

OR if you want as String array and comma separated then

@ValueMapValue private String[] languages; public String[] getLanguages (){ return languages; }${model.languages}

Thanks

Abhishekty
Level 4
December 18, 2024

Thanks @mukeshyadav_ , I'll try it this way, it seems like it will fix the issue.