SBX - Search With Button

SBX - Forum Post Title

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

Microsoft Dynamics CRM Forum

Luca Genovese asked a question on 15 May 2017 2:26 PM
My Badges

Question Status

Suggested Answer

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.

Reply
Durgaprasad Katari responded on 13 Nov 2017 7:06 AM
My Badges

Nice post. This really helps me.

Reply
Dan Patrichi responded on 26 Feb 2018 11:52 AM

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

Reply
Durgaprasad Katari responded on 27 Feb 2018 2:07 AM
My Badges

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. 

Reply
Mohammed Fakhri responded on 15 Oct 2018 1:09 AM
My Badges

Thank you so much Luca.

Reply
Ravi Kashyap responded on 15 Oct 2018 8:03 PM
Suggested Answer

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.

Reply
Luca Genovese responded on 16 Oct 2018 1:35 AM
My Badges

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

Reply
Jgaddam responded on 9 Jun 2019 8:14 PM

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

Reply
Dmytro Rutkovskyi responded on 11 Jun 2019 1:11 PM
My Badges
Suggested Answer

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.

Reply
Ravi Kashyap responded on 15 Oct 2018 8:03 PM
Suggested Answer

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.

Reply
Dmytro Rutkovskyi responded on 11 Jun 2019 1:11 PM
My Badges
Suggested Answer

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.

Reply

SBX - Two Col Forum

SBX - Migrated JS