Unable to save dialog because of required fields | Community
Skip to main content
New Participant
January 4, 2024
Solved

Unable to save dialog because of required fields

  • January 4, 2024
  • 3 replies
  • 1036 views

I have a component with a dropdown. Based on the selection, the fields will be shown in the dialog box. 

Suppose there are two options, option 1 and option 2.

On selecting option 1, a field shows up which is a required field.

 

Now, the issue is that even if I don't select any value from the dropdown, I'm not able to save the dialog box because of the "required=true" condition on the option 1 field. How can I resolve this issue?

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 pulkitvashisth

To have one item in a multifield component as default when you add the component on the page, you can listen to the loadcontent event fired by the Multifield after the content has been loaded and use the addItem() method. Here is an example of how you can achieve this:

<paths  jcr:primaryType="cq:Widget"  fieldLabel="Select Paths"  name="./paths"  xtype="multifield">  
    <fieldConfig  jcr:primaryType="nt:unstructured"  xtype="pathfield" />  
    <listeners  jcr:primaryType="nt:unstructured"  loadcontent="function (field, record)  
    {  
        if (record.get ('paths') == undefined)  
        {  
            field.addItem (); 
            field.doLayout ();  
        }  
    }" /> 
</paths>
 

In this example, the loadcontent event is used to check if the paths record is undefined. If it is, the addItem() method is called to add a new item, and doLayout() is called to refresh the layout. This will ensure that at least one item is displayed in the dialog by default. Please replace paths with your field name.

3 replies

kautuk_sahni
Employee
January 8, 2024

@goyalkritika Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.

Kautuk Sahni
pulkitvashisth
pulkitvashisthAccepted solution
New Participant
January 5, 2024

To have one item in a multifield component as default when you add the component on the page, you can listen to the loadcontent event fired by the Multifield after the content has been loaded and use the addItem() method. Here is an example of how you can achieve this:

<paths  jcr:primaryType="cq:Widget"  fieldLabel="Select Paths"  name="./paths"  xtype="multifield">  
    <fieldConfig  jcr:primaryType="nt:unstructured"  xtype="pathfield" />  
    <listeners  jcr:primaryType="nt:unstructured"  loadcontent="function (field, record)  
    {  
        if (record.get ('paths') == undefined)  
        {  
            field.addItem (); 
            field.doLayout ();  
        }  
    }" /> 
</paths>
 

In this example, the loadcontent event is used to check if the paths record is undefined. If it is, the addItem() method is called to add a new item, and doLayout() is called to refresh the layout. This will ensure that at least one item is displayed in the dialog by default. Please replace paths with your field name.

Madhur-Madan
New Participant
January 4, 2024

Hi @goyalkritika ,

 

In your client-side JavaScript file, listen for changes in the dropdown and toggle the 'required' attribute of the field accordingly.

(function($, $document) { "use strict"; $document.on("foundation-contentloaded", function() { // Function to handle dropdown change function handleDropdownChange() { var dropdownValue = $("coral-select[name='./dropdown']").val(); var requiredField = $("coral-textfield[name='./requiredField']"); // Set 'required' attribute based on the selected value requiredField.prop("required", dropdownValue === "option1"); } // Event listener for dropdown change $("coral-select[name='./dropdown']").on("change", handleDropdownChange); // Trigger the event on dialog load (initial state) handleDropdownChange(); }); })($, $(document));

Include the client library in your AEM component's dialog by referencing it in your component's dialog's using extraClientlibs 

<dialog jcr:primaryType="nt:unstructured" ... extraClientlibs="[yourproject.validation]" > <!-- Dialog fields and structure --> </dialog>

 

New Participant
January 4, 2024

I modified the above js to this - 

where addtocart is the name of option 1 and buttonlabel is the name of mandatory field. Still it is not working. I've even added the clientlib to component level. What am I doing wrong?