AdxStudio: Make fields required or not required using Javscript

As of writing, business rules are not yet supported in AdxStudio / Microsoft Portals.

With this, if we need the functionalities of business rules such as making fields required and not required, we need to use javascript / jQuery.

Below are my codes for toggling this behavior.

You can use this on EntityForms, WebFormSteps, and any area in Adxstudio / Microsoft Portals where custom javscript is accepted.

 


var MakeRequired = function (fieldName) {
    try {
       if ($("#" + fieldName) != undefined) {
          $("#" + fieldName).prop('required', true);
          $("#" + fieldName).closest(".control").prev().addClass("required");

          // Create new validator
          var Requiredvalidator = document.createElement('span');
          Requiredvalidator.style.display = "none";
          Requiredvalidator.id = fieldName + "Validator";
          Requiredvalidator.controltovalidate = fieldName;
          Requiredvalidator.errormessage = "<a href='#" + fieldName + "_label'>" + $("#" + fieldName + "_label").html() + " is a required field.</a>";
          Requiredvalidator.initialvalue = "";
          Requiredvalidator.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(Requiredvalidator);
       }
    }
    catch (error) {
       errorHandler(error);
    }
 }

 var MakeNotRequired = function (fieldName) {
    try {
       if ($("#" + fieldName) != undefined) {
          $("#" + fieldName).closest(".control").prev().removeClass("required");
          $("#" + fieldName).prop('required', false);

          for (i = 0; i < Page_Validators.length; i++) {
             if (Page_Validators[i].id == fieldName + "Validator") {
                Page_Validators.splice(i);
             }
          }
       }
    }
    catch (error) {
       errorHandler(error);
    }
 }
Advertisements

One Comment Add yours

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s