Skip to main content

Notifications

Power Pages - General Discussions
Suggested answer

make attachment mandatory if checkbox is selected

Like (0) ShareShare
ReportReport
Posted on 3 Sep 2024 10:45:37 by 333
I am trying to make the attachment field mandatory only if checkbox is selected.
I tried below JScript but not working.
 
/*for attachment field*/
document.getElementById("cr4f0_certificatecheckbox").addEventListener('change', function(){
  if(this.checked)
  {
  document.getElementById("AttachFileLabel").required = false;
  }
  else
  {
  document.getElementById("AttachFileLabel").required = true;
  }
});
 
 
Categories:
  • Suggested answer
    Inogic Profile Picture
    Inogic 861 on 06 Sep 2024 at 10:08:50
    make attachment mandatory if checkbox is selected
    Hi,
    To prevent a user from submitting a form based on specific conditions in Power Pages, relying solely on the "required" field may not be effective, especially if the field is not marked as required in CRM.
    A possible approach is to implement custom logic to control the form submission. For example, if a checkbox is selected but the user has not attached a document, you can create a secondary button to validate this condition.
    Here’s how it works:
    • The secondary button checks whether the document is attached when the checkbox is checked.
    • If the document is not attached, the button triggers an alert notifying the user with a message like "Please attach the document."
    • The actual submit button is hidden and will only be triggered by the secondary button if the condition is met.
    This ensures the form cannot be submitted until the required conditions are satisfied.
     
    Hope this helps.
     
    Thanks!
    Inogic Professional Services: Power Platform/Dynamics 365 CRM
    An expert technical extension for your techno-functional business needs
    Drop an email at crm@inogic.com 
    Service: https://www.inogic.com/services/ 
    Tips and Tricks: https://www.inogic.com/blog/ 
  • Suggested answer
    GWham Profile Picture
    GWham 54 on 05 Sep 2024 at 10:09:12
    make attachment mandatory if checkbox is selected
        //eg. addValidator("customerid", "Customer")
        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); });
        }
    Try to add a custom validator for the upload field when the checkbox is selected.

    You will also need to remove it when deselected.
     
    //eg. removeValidator("customerid")
    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");
    }
     
     
     
     
     
  • Suggested answer
    Fubar Profile Picture
    Fubar 7,643 on 04 Sep 2024 at 22:30:55
    make attachment mandatory if checkbox is selected
    For the mandatory (red asterisk) you will probably have to add the class to the parent not the on the label tag directly
    $("#NewAttachFileLabel").parent().addClass("required");
    Note: this will only put the asterisk not force it to be mandatory, to check it has been populated you will need to check if the field is populated in a custom validator https://learn.microsoft.com/en-us/power-pages/configure/add-custom-javascript#additional-client-side-field-validation
     

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

End of Year Newsletter…

End of Year Community Newsletter…

Tuesday Tip #12 Start your Super User…

Welcome to a brand new series, Tuesday Tips…

Tuesday Tip #13 Writing Effective Answers…

Welcome to a brand new series, Tuesday Tips…

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 144,769

#2
RandyHayes Profile Picture

RandyHayes 76,287

#3
Pstork1 Profile Picture

Pstork1 64,466

Leaderboard
Loading started