cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
T1ago
Helper I
Helper I

Form Attachment mandatory depending on field

Hi,

I need to make the file attachment mandatory depending on a dropdown option.

 

I have made this for other fields by using the validators (similar to this https://powerusers.microsoft.com/t5/Power-Apps-Portals/Make-field-required-in-Portal-based-on-value-...)

However I can't quite do it with the file attachment.

 

Is it possible to make a verification Portal side only?

1 ACCEPTED SOLUTION

Accepted Solutions
T1ago
Helper I
Helper I

Managed to figure it out, using the validators. For anyone that might need it:

var fileRequired = 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 + "'> File is mandatory";

                
                Requiredvalidator.initialvalue = "";
                Requiredvalidator.evaluationfunction = function() {
                    var inputFile = $('#AttachFile').val();
                    if (inputFile == "") {
                        return false;
                    } else {
                        return true;
                    }
                };

                // Add the new validator to the page validators array:
                Page_Validators.push(Requiredvalidator);
            }
        } catch (error) {
            errorHandler(error);
        }
    }

    var fileNotRequired = 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);
        }
    }

 

And then use when needed (fieldName is <input> ID of the file upload):

fileNotRequired("AttachFile");

fileRequired("AttachFile");

View solution in original post

1 REPLY 1
T1ago
Helper I
Helper I

Managed to figure it out, using the validators. For anyone that might need it:

var fileRequired = 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 + "'> File is mandatory";

                
                Requiredvalidator.initialvalue = "";
                Requiredvalidator.evaluationfunction = function() {
                    var inputFile = $('#AttachFile').val();
                    if (inputFile == "") {
                        return false;
                    } else {
                        return true;
                    }
                };

                // Add the new validator to the page validators array:
                Page_Validators.push(Requiredvalidator);
            }
        } catch (error) {
            errorHandler(error);
        }
    }

    var fileNotRequired = 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);
        }
    }

 

And then use when needed (fieldName is <input> ID of the file upload):

fileNotRequired("AttachFile");

fileRequired("AttachFile");

Helpful resources

Announcements
Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

Carousel News & Announcements 768460.png

What's New in the Community?

Check out the latest News & Events in the community!

MPP IDEAS updated 768x460.png

Ideas

Discover ideas and concepts from users like you for how to use Power Pages and take your work to the next level.

Top Solution Authors
Users online (3,899)