web
You’re offline. This is a read only version of the page.
close
Skip to main content
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)
  • ScheumanRJ Profile Picture
    76 on at
    RE: [HOW TO] Enable/Disable mandatory fields on portal form

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

  • Suggested answer
    Dmytro Rutkovskyi Profile Picture
    1,835 on at
    RE: [HOW TO] Enable/Disable mandatory fields on portal form

    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.

  • Jgaddam Profile Picture
    5 on at
    RE: [HOW TO] Enable/Disable mandatory fields on portal form

    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

  • Luca Genovese Profile Picture
    on at
    RE: [HOW TO] Enable/Disable mandatory fields on portal form

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

  • Suggested answer
    RaviKashyap Profile Picture
    55,410 Moderator on at
    RE: [HOW TO] Enable/Disable mandatory fields on portal form

    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.

  • Mohammed Fakhri Profile Picture
    402 on at
    RE: [HOW TO] Enable/Disable mandatory fields on portal form

    Thank you so much Luca.

  • Durgaprasad Profile Picture
    80 on at
    RE: [HOW TO] Enable/Disable mandatory fields on portal form

    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. 

  • danpatrichi99@gmail.com Profile Picture
    on at
    RE: [HOW TO] Enable/Disable mandatory fields on portal form

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

  • Durgaprasad Profile Picture
    80 on at
    RE: [HOW TO] Enable/Disable mandatory fields on portal form

    Nice post. This really helps me.

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…

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Aric Levin - MVP Profile Picture

Aric Levin - MVP 2 Moderator

#2
MA-04060624-0 Profile Picture

MA-04060624-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans