Expand my Community achievements bar.

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

Avatar

Level 4

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


Topics

Topics help categorize Community content and increase your ability to discover relevant content.

0 Replies