Skip to main content

Notifications

Community site session details

Community site session details

Session Id : 0OgPhzAmZtYFGBWbWqVYEh
Power Pages - General Discussions
Suggested answer

How to remove a required field from form using javascript?

Like (2) ShareShare
ReportReport
Posted on 27 Aug 2024 17:28:49 by 18
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:
  • Suggested answer
    SaiRT14 Profile Picture
    1,966 Super User 2025 Season 1 on 01 Nov 2024 at 19:55:16
    How to remove a required field from form using javascript?
    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.
  • Suggested answer
    GWham Profile Picture
    58 Super User 2025 Season 1 on 28 Aug 2024 at 08:26:54
    How to remove a required field from form using javascript?
    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
    Fubar Profile Picture
    7,852 Super User 2025 Season 1 on 27 Aug 2024 at 21:58:47
    How to remove a required field from form using javascript?
    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).

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

Understanding Microsoft Agents - Introductory Session

Confused about how agents work across the Microsoft ecosystem? Register today!

Warren Belz – Community Spotlight

We are honored to recognize Warren Belz as our May 2025 Community…

Congratulations to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Power Pages - General Discussions

Overall leaderboard