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 / Set File Field Require...
Power Pages
Unanswered

Set File Field Required with JavaScript

(0) ShareShare
ReportReport
Posted on by

Hi All!!

Within Power Pages I am trying to set the attach a file as required on a basic form when a Change Request = Bank Account Information with JavaScript.

 

I have tried the following with JavaScript trying to set adx_attachfilerequiredfield as true but cannot get it to work. Any ideas for the script would be appreciated!

Categories:
I have the same question (0)
  • CU20091748-0 Profile Picture
    39 on at

    I think you would need to bind the function to the change event of the choice field. You would have this in your main function:

    function updateRequiredClass(); {
    
    //retrieve value of fields
    
    
    
    //define condition under which the dependent field should be required
    
    
    }
    
    
    $("#choice_field").change(function() {
    
    
    
    updateRequiredClass();
    
    
    
    });

     

  • oliver.rodrigues Profile Picture
    9,368 Most Valuable Professional on at

    Hi, you should add an actual form validation and not just set the class: Add custom JavaScript to a form | Microsoft Learn

     

  • GWham1 Profile Picture
    on at

    Something like the following might work. You will need to call the addValidator function with your file field ID when the change request is selected.

     

     

    $(document).ready(function() {
     // trigger on change?
     $("#changerequest").click(function() {
     if ($(this).val('Bank Account')) {
     // set mandatory
     addValidator ("gw_file", "Select a file");
     }
     });
    
     // add validator
     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); });
     }
    
    });

     

     

  • Fubar Profile Picture
    8,338 Super User 2025 Season 2 on at

    @GWham1 with validators it is probably better to add them first, and then put the logic inside the validator e.g. field1 = "x" then .... if you add the validator inside an onchange then you also need to either remove the validator or check for its existence to avoid the same validator getting added multiple times (e.g. when someone toggles the field the onchange is on) - when this happens the each validator object gets called and the user sees multiple of the same error messages.

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