Skip to main content

Notifications

Customer experience | Sales, Customer Insights,...
Suggested answer

Portals-make fields mandatory Dynamically

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

How to make fields mandatory conditionally(like based on some field value) with out using any code? Is this possible ?

  • Suggested answer
    Fubar Profile Picture
    Fubar 2,752 on at
    RE: Portals-make fields mandatory Dynamically

    The trigger is different when the user presses the next/previous step buttons - Rather than using Page_validator it calls webFormClientValidate (you put your pass/fail code inside where the comments are in the example) see the following link

    docs.microsoft.com/.../add-custom-javascript

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Portals-make fields mandatory Dynamically

    Thanks LA for your answer. I am using this code in Web-From Step of Edit Type. Which part of code that is different for entity form and web form step(I got confused here).

    Note: i included this code in function and am reusing this function as i am dealing with  more fields.

  • Suggested answer
    Fubar Profile Picture
    Fubar 2,752 on at
    RE: Portals-make fields mandatory Dynamically

    Note, the above code is for an Entity form, there is slightly different code if using a Web Form step (the link in my previous post also has this).

    If its a CRM cache issue, clear the cache <your portal url>/_services/about , if its a Chrome cache issue F12 with the debugger open then right click on the refresh button on the browser (chrome will then give you a clear hard cache option.  (If you inspect the HTML in the debugger you can search for the code you updated to see if it has flowed through to the browser)

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Portals-make fields mandatory Dynamically

    Yes. I have used the same code but sometimes its not working user can able to proceed without providing the data.But its happening sometimes only.I am not  sure the reason.It might be the Cache issue?

  • Suggested answer
    Fubar Profile Picture
    Fubar 2,752 on at
    RE: Portals-make fields mandatory Dynamically

    Not possible without adding code (unless you are in a Web Form and have a conditional step with 2 different forms one with the mandatory and the other without it).

    With JavaScript you add the 'required' class to the CSS of the element (to show the red asterix), then add a custom validator to put your logic for when it passes/fails the mandatory test.  The code below adds a custom validator to validate that the emailaddress has been populated if email has been specified as a preferred contact method (as per https://docs.microsoft.com/en-us/powerapps/maker/portals/configure/add-custom-javascript

     

    if (window.jQuery) {

      (function ($) {

         $(document).ready(function () {

            if (typeof (Page_Validators) == 'undefined') return;

            // Create new validator

            var newValidator = document.createElement('span');

            newValidator.style.display = "none";

            newValidator.id = "emailaddress1Validator";

            newValidator.controltovalidate = "emailaddress1";

            newValidator.errormessage = "<a href='#emailaddress1_label'>Email is a required field.</a>";

            newValidator.validationGroup = ""; // Set this if you have set ValidationGroup on the form

            newValidator.initialvalue = "";

            newValidator.evaluationfunction = function () {

               var contactMethod = $("#preferredcontactmethodcode").val();

               if (contactMethod != 2) return true; // check if contact method is not 'Email'.

               // only require email address if preferred contact method is email.

               var value = $("#emailaddress1").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='#emailaddress1_label']").on("click", function () { scrollToAndFocus('emailaddress1_label','emailaddress1'); });

         });

      }(window.jQuery));

    }

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

News and Announcements

Announcing Category Subscriptions!

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,359 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,370 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans