Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Making a dialog field mandatory only when visible

Avatar

Level 3

I have a drop down with 4 options (a, b, c, d). Upon selection of option b, I show certain fields and hide these fields when rest 3 options are selected.

 


Problem: I need to make these conditional items mandatory only when they are visible (i.e, option b is selected). Adding the flag
required="{Boolean}true" is not solving the purpose as AEM expect it to be filled up even when it is hidden.

 

Thanks.

 

 

 

@arunpatidar 

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @naveen_27_05 

 

You can use Foundation Validator.You can follow this blog https://aemhints.com/2020/11/02/validating-dialog-value/

Here, if you check, field is checked against regex and if regex matches, js function return nothing which means field is valid otherwise it returns some error message. You can use this same concept where instead of regex you can write your logic to check if another filed is hidden or not using jquery and choose to return error or not.

 

Hope it helps!

 

Let me know if you need more information. 

 

Thanks,

Nupur

View solution in original post

4 Replies

Avatar

Community Advisor

There are no inbuilt JCR properties to achieve this. However, you can still accomplish this by loading a js through extra client libs. The idea here is to add the "aria-required, true" property only when the show field is valid.


Avatar

Correct answer by
Community Advisor

Hi @naveen_27_05 

 

You can use Foundation Validator.You can follow this blog https://aemhints.com/2020/11/02/validating-dialog-value/

Here, if you check, field is checked against regex and if regex matches, js function return nothing which means field is valid otherwise it returns some error message. You can use this same concept where instead of regex you can write your logic to check if another filed is hidden or not using jquery and choose to return error or not.

 

Hope it helps!

 

Let me know if you need more information. 

 

Thanks,

Nupur

Avatar

Community Advisor

@naveen_27_05 

Try below code

 

 

(function($, Coral) {
    "use strict";
    var registry = $(window).adaptTo("foundation-registry");
    registry.register("foundation.validation.validator", {
        selector: "[name='./selectField']",
        validate: function(element) {
            hideFields(element.value);
        }
    });

    $(document).on("dialog-ready", function() {
       hideFields($("[name='./selectField']").val());
    });

    const hideFields = (value) => {
        if(value == 'hide') {
        	$("[name='./title']").removeProp("required").removeAttr("aria-required").closest(".coral-Form-fieldwrapper").hide();
        	// Add more fields to hide
        } else {
            $("[name='./title']").prop("required", "required").attr("aria-required", "true").closest(".coral-Form-fieldwrapper").show();
            // Add more fields to show
        }
    };
})(jQuery, Coral);