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

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Pages / How to remove a requir...
Power Pages
Unanswered

How to remove a required field from form using javascript?

(2) ShareShare
ReportReport
Posted on by 25
Hello everyone,
 
I have a form for a Dataverse table that I want to use for different cases. In some cases, some fields on the form are not relevant and I want to remove these fields only in these cases. I tried searching for the way to do it but they only mentioned the case where the fields are not required. In my case, all these fields/columns are set as "business required" because users should fill in all relevant fields before submitting, so even if I can dynamically hide them with javascript, when I click submit form, the error message "Fields ... are required" still shows.
 
Is there a way I can dynamically remove these fields with javascript? It would be too much work to create and customize a seperated form for each case.
 
Thanks in advance!
Categories:
I have the same question (0)
  • Suggested answer
    Fubar Profile Picture
    8,338 Super User 2025 Season 2 on at
    Approach it from the other direction, make the field optional, then display it as mandatory (when and as needed) on your form, and push a custom Validator on to the Validator object (the validator code will display as a normal error)
     
    (you will also find other code in the forums for making fields required),
     
    Also if you have a form where it should always be mandatory and another where it should not you can also add a Metadata record to the form that it needs to be mandatory (the metadata record his has a check box to make it required the form).
  • Suggested answer
    GWham Profile Picture
    58 Moderator on at
    function removeValidator(fieldName) {
      var count = 0;
      for(var i =0; i < Page_Validators.length - count; i++){
        if (Page_Validators[i - count].id == "RequiredFieldValidator" + fieldName) {
          Page_Validators.splice(i - count, 1);
          count++;
        }
      }
      $("#" + fieldName + "_label").parent().removeClass("required");
    }
    You can remove validation using the above JavaScript.
     
    As Fubar mentioned, id maybe swap the logic and make them optional and then add a custom validator instead of removing or make fields required via metadata.
     
        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 = "";
            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); });
        }
     
     
     
  • Suggested answer
    SaiRT14 Profile Picture
    1,990 Super User 2025 Season 2 on at
    Easiest in case of model-driven app - Create Conditional Business Rules: 
    • If you're working in a model-driven app, use business rules to dynamically set the fields as required or not based on conditions that reflect your use cases.
    • Set conditions in the business rule to mark fields as required when certain criteria are met.

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

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Pages

#1
Jerry-IN Profile Picture

Jerry-IN 71

#2
Fubar Profile Picture

Fubar 62 Super User 2025 Season 2

#3
sannavajjala87 Profile Picture

sannavajjala87 31

Last 30 days Overall leaderboard