Skip to main content

Notifications

Microsoft Dynamics CRM forum
Suggested answer

Portals-make fields mandatory Dynamically

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 ?

Categories:
  • Suggested answer
    Fubar Profile Picture
    Fubar 2,750 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,750 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,750 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

Quick Links

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,524 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,493 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans
Liquid error: parsing "/forums/thread/details/?threadid=%27nvOpzp;%20AND%201=1%20OR%20(%3C%27%22%3EiKO))," - Too many )'s.