cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Mikello-6626
Helper III
Helper III

Make field required in Portal based on value selected in another field

I am using Dataverse/Model driven app but displaying on a Portal.

 

I have two fields:

myschema_OtherPresentation  - Custom Choice field (Yes, No, Blank) - appears as radio button

myschema_DescribethisOtherPresentation - - Text field

 

If someone chooses "Yes" for myschema_OtherPresentation, then make myschema_DescribethisOtherPresentation required. Both fields are on the same tab and there is a Next button after this tab.  However, choosing Yes/No does not change a section or tab etc. so I believe I can't use Advanced Form Steps. 

 

Right now I am using this Javascript but don't know what is wrong with this:

https://docs.microsoft.com/en-us/powerapps/maker/portals/configure/add-custom-javascript 

 

 

 

 

 

 

 

 

      $(document).ready(function () {
         if (typeof (Page_Validators) == 'undefined') return;
         // Create new validator
         var newValidator = document.createElement('span');
         newValidator.style.display = "none";
         newValidator.id = "myschema_DescribethisOtherPresentationValidator";
         newValidator.controltovalidate = "myschema_DescribethisOtherPresentation";
         newValidator.errormessage = "<a href='#myschema_DescribethisOtherPresentation_label'>Describe this Other Presentation is a required field.</a>";
         newValidator.validationGroup = ""; // Set this if you have set ValidationGroup on the form
         newValidator.initialvalue = "";
         newValidator.evaluationfunction = function () {
            var contactMethod = $("#myschema_OtherPresentation").val();
            if (contactMethod != 669080000) return true; // check if OtherPresentation is not 'Yes'
            // only require DescribethisOtherPresentation if OtherPresentation is Yes.
            var value = $("#myschema_DescribethisOtherPresentation").val();
            if (value == null || value == "") {
            return false;
            } else {
               return true;
            }
         };
 
         // Add the new validator to the page validators array:
         Page_Validators.push(newValidator);
 
         // Wire-up the click event handler of the validation summary link
         $("a[href='#myschema_DescribethisOtherPresentation_label']").on("click", function () { scrollToAndFocus('myschema_DescribethisOtherPresentation_label','myschema_DescribethisOtherPresentation'); });
      });

 

 

 

 

 

 

 

 

 

Yes value is 669,080,000 in my option set

No value is 669,080,001

 

 

@OliverRodrigues 

2 REPLIES 2
Mikello-6626
Helper III
Helper III

Any replies appreciated

Manar
Resolver IV
Resolver IV

Hello there, not sure if its still relevant but i faced same issue and i had to use validator to enforce required class , check below codes and what i am doing i am checking im360_thiscompanyis field and by its value i am displaying other fields 

 

if (window.jQuery) {
    (function ($) {
        debugger;
        $(document).ready(function () {
           $('#im360_thiscompanyis').change(function () {
               debugger;

                if ($('#im360_thiscompanyis').length) {
                    showHideControls();
           
                }
            });
        });
    }(window.jQuery));
}
//Pass Id to makeRequired & makeNotRequired function
function showHideControls() {
    debugger;

    var CompanyType = $("#im360_thiscompanyis").val();
    if (CompanyType == 1) //if it’s a public company 
    {
        makeRequired('im360_stocksymbol');
        makeNotRequired('im360_yearbusinesswasestablished');
        makeNotRequired('im360_monthbusinessestablished');
        makeNotRequired('im360_privatecompanytype');
    }
    else if (CompanyType == 2) //if privet company show private company type and fields 
    {
        makeRequired('im360_yearbusinesswasestablished');
        makeRequired('im360_monthbusinessestablished');
        makeRequired('im360_privatecompanytype');
        makeNotRequired('im360_stocksymbol');
    }
}
//Make field required.
function makeRequired(fieldName) {
    try {
        if ($("#" + fieldName) != undefined) {
            $("#" + fieldName).prop('required', true);
            $("#" + fieldName).closest(".control").prev().addClass("required");

            // Create new validator
            var Requiredvalidator = document.createElement('span');
            Requiredvalidator.style.display = "none";
            Requiredvalidator.id = fieldName + "Validator";
            Requiredvalidator.controltovalidate = fieldName;
            Requiredvalidator.errormessage = "<a href='#" + fieldName + "_label'>" + $("#" + fieldName + "_label").html() + " is a required field.</a>";
            Requiredvalidator.initialvalue = "";
            Requiredvalidator.evaluationfunction = function () {
                var value = $("#" + fieldName).val();
                if (value == null || value == "") {
                    return false;
                } else {
                    return true;
                }
            };

            // Add the new validator to the page validators array:
            Page_Validators.push(Requiredvalidator);
        }
    }
    catch (error) {
        errorHandler(error);
    }
}

function makeNotRequired(fieldName) {
    //Clean up previous validator <span> if present
    if ($('#ValidationSummaryEntityFormView').length) {
        $('#ValidationSummaryEntityFormView').hide();
    }

    try {
        if ($("#" + fieldName) != undefined) {
            $("#" + fieldName).closest(".control").prev().removeClass("required");
            $("#" + fieldName).prop('required', false);

            for (i = 0; i < Page_Validators.length; i++) {
                if (Page_Validators[i
                ].id == fieldName + "Validator") {
                    Page_Validators.splice(i);
                }
            }
        }
    }
    catch (error) {
        errorHandler(error);
    }
}

 

 

Helpful resources

Announcements
User Group Leader Meeting January 768x460.png

Calling all User Group Leaders!

Don't miss the User Group Leader meetings on January, 24th & 25th, 2022.

Community Connections 768x460.jpg

Community & How To Videos

Check out the new Power Platform Community Connections gallery!

Users online (1,552)