web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

[HOW TO] Enable/Disable mandatory fields on portal form

(7) ShareShare
ReportReport
Posted on by

Hi all, 

if you want to change client side the mandatory status of a field, you can use these two functions:

//eg. addValidator("customerid", "Customer")
function addValidator(fieldName, fieldLabel) {


    if (typeof (Page_Validators) == 'undefined') return;
    // Create new validator
    $("#" + fieldName + "_label").parent().addClass("required");

    var newValidator = document.createElement('span');
    newValidator.style.display = "none";
    newValidator.id = "RequiredFieldValidator" + fieldName;
    newValidator.controltovalidate = "casetypecode";
    newValidator.errormessage = "<a href='#" + fieldName + "_label'>" + fieldLabel + " is a mandatory field.</a>";
    newValidator.validationGroup = "";
    newValidator.initialvalue = "";
    newValidator.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(newValidator);

    // Wire-up the click event handler of the validation summary link
    $("a[href='#" + fieldName + "_label']").on("click", function () { scrollToAndFocus(fieldName + '_label', fieldName); });
}

//eg. removeValidator("customerid")
function removeValidator(fieldName) {
    $.each(Page_Validators, function (index, validator) {
        if (validator.id == "RequiredFieldValidator" + fieldName) {
            Page_Validators.splice(index, 1);
        }
   });

    $("#" + fieldName + "_label").parent().removeClass("required");
}


This code works only if the field was not previously mandatory on the CRM. I suggest you to use this code in order to manage mandatory fields directly client side.

Hope this helps.

*This post is locked for comments

I have the same question (0)
  • Durgaprasad Profile Picture
    80 on at

    Nice post. This really helps me.

  • danpatrichi99@gmail.com Profile Picture
    on at

    Thank you so much Luca. It saved me a lot of time.

  • Durgaprasad Profile Picture
    80 on at

    I have added this code in the web form step. But for some reason it is not working. 

    The form is posting back to server and only server side fields are showing as mandatory.

    Any idea on this. Is there any configurations required. 

  • Mohammed Fakhri Profile Picture
    402 on at

    Thank you so much Luca.

  • Suggested answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    Hi,

    You don't need to write code to make the field mandatory on Portals, You can simply add the metadata configuration for the attribute and select the field mandatory.

    refer this blog: nishantrana.me/.../using-entity-form-metadata-to-configure-the-entity-form-in-portal-in-dynamics-365

    Hope this helps.

  • Luca Genovese Profile Picture
    on at

    That's true. But if you want to set a field mandatory under specific conditions you need to write client side code.

  • Jgaddam Profile Picture
    5 on at

    Hello Luca,

    I used the above code to set a field required(option set rendered as radio buttons) even if there is data in the field it is still saying the field is required can you please help me resolve my issue

  • Suggested answer
    Dmytro Rutkovskyi Profile Picture
    1,835 on at

    You need discover how to properly check data presence in JavaScript.

    So instead of just checking 

    $("#"+fieldname).val()

    you need to look for more specific way to get value, like:

    $("#"+fieldname").find("input[checked]").attr("value")

    which return you value of checked radio button, or slightly change the condition to see if you have at least one checked radio.

  • ScheumanRJ Profile Picture
    76 on at

    Thanks Luca!  This worked great and saved me a lot of time.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans