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 / Web Form Step - Preven...
Power Pages
Unanswered

Web Form Step - Prevent Submit

(0) ShareShare
ReportReport
Posted on by 236

Hello,

 

I have a Web Form with several different steps.

 

The second to the last step surfaces a form that only displays a subgrid.

I would like to prevent the Submit button from executing the submit/save action if there are no related records in the subgrid.

Previously, I was able to accomplish this by inserting the following code into the Web Form Step -> Form Options -> Custom Javascript.

 

if (window.jQuery) {
 (function ($) {
 if (typeof webFormClientValidate != "undefined") {
 var originalValidationFunction = webFormClientValidate;
 if (
 originalValidationFunction &&
 typeof originalValidationFunction == "function"
 ) {
 webFormClientValidate = function () {
 originalValidationFunction.apply(this, arguments);
 // do your custom validation here
 const tableBody = document.querySelector(
 ".view-grid > table > tbody"
 );

 // the tbody element isn't created if there are no records.
 if (!tableBody) {
 alert("You must add at least one requested service.");
 // return false; // to prevent the form submit you need to return false
 return false;
 }
 // end custom validation.
 return true;
 };
 }
 }
 })(window.jQuery);
}

The code was originally lifted from here

 

When the user would click Submit with 0  records added, the standard Alert would appear informing the user that at least 1 record must be included in the subgrid.  Worked like a charm.

 

However, today I noticed that this is no longer working.   The form just submits and doesn't seem to pay any mind to this custom validator.

 

Can someone please assist?  The goal is to prevent the Submit button from executing if there are no records in the subgrid.

 

NewcombR_0-1615831854630.png

 

Categories:
I have the same question (0)
  • OOlashyn Profile Picture
    3,496 Most Valuable Professional on at

    Hi @NewcombR,

    Can you inspect the page to make sure that first your custom JS is added to the page and second if the tbody is actually missing? Might be that there are either changes in the rendering of the page ie now for some reason tbody is present even if there is no record. Also please check if the class of the wrapping div is indeed view-grid - might be that it was updated or missing something. If you don't know how to inspect the page check this article.

  • Ryan S Newcomb Profile Picture
    236 on at

    Thanks for your response.  I am able to confirm that the script is loaded, and that the tbody is missing.

     

    Here's a screenshot from the DOM (when there are no records in the grid).

     

    NewcombR_0-1615838913079.png

     

     

    Here's a screenshot of me querying in the console when there are no records in the grid.

    NewcombR_1-1615839012868.png

     

    Here's also a screenshot of where the Web Form Step's Custom Javascript is loaded in the DOM.

    NewcombR_2-1615839143597.png

     



    When I add a record in the subgrid, I can verify that the tbody is created.  Here is a screenshot of me using the console when there is 1 record in the grid.

     

    NewcombR_3-1615839373341.png

     

     

    Now, there is only 1 element on the CDS/Dataverse form used in this web step: the subgrid.   Would this prevent the validator from being called on submit?

  • OOlashyn Profile Picture
    3,496 Most Valuable Professional on at

    Hmm... Looks like everything is correct. Can you add console.log with tableBody after you get it to see if it is actually null and if the validation function is triggering?

     

  • Ryan S Newcomb Profile Picture
    236 on at

    I added the console.log(tableBody) after declaring the tableBody variable.  It doesn't appear that the function is being called as nothing is logged.  Can't even get it to step through with debugger. 

     

    debugger;
    if (window.jQuery) {
     (function ($) {
     if (typeof webFormClientValidate != "undefined") {
     var originalValidationFunction = webFormClientValidate;
     if (
     originalValidationFunction &&
     typeof originalValidationFunction == "function"
     ) {
     webFormClientValidate = function () {
     originalValidationFunction.apply(this, arguments);
     // do your custom validation here
    
     const tableBody = document.querySelector(".view-grid > table > tbody");
     console.log(tableBody);
    
     // the tbody element isn't created if there are no records.
     if (!tableBody) {
     // TODO : Add in a better way to inform the user.
     alert("You must add at least one requested service.");
     return false;
     }
    
     // return false; // to prevent the form submit you need to return false
     // end custom validation.
     return true;
     };
     }
     }
     })(window.jQuery);
    }
  • Fubar Profile Picture
    8,338 Super User 2025 Season 2 on at

    Think it is the Page Template that you are using on your Web Page definition.  I was able to reproduce the problem using the Full Page template, when using this template the following line is returning 'undefined' and so all the logic is being skipped 

     

     

    if (typeof (webFormClientValidate) != 'undefined') {

     

     

    If you change the Web Page's Page Template to Web Form then the line returns 'function' (it could also possibly work with other Page Templates - but I didn't try any others)

     

    Note: if you are using a blank/custom portal Web Form template is not present by default, but you can create one, by creating a Page Template that is 'rewrite' and using the following url  ~/Pages/WebForm.aspx

  • Ryan S Newcomb Profile Picture
    236 on at

    Interesting.  I'm currently using a custom Page Template that is using a custom Web Template, which is extending a more generic Web Template.

     

    Based on the configuration I have below, are you suggesting that I update the Page Template's type to Rewrite

     

    Here is the current Page Template config:

     

    NewcombR_0-1615859076660.png

     

     

     

    The Web Template source ("Consumer Referral - Web Form");

    {% extends 'Page Web Form Template' %}
    
    {% block page-heading %}
    
    {% if request.params.refid %}
     {% assign consumerid = request.params.refid %}
     {% assign consumer = entities['csa_consumer'][consumerid] %}
    
     {% if consumer %}
     <div class="page-heading">
     {% include 'Consumer Referral Page Web Form Breadcrumbs' recordid:consumerid consumername:consumer.csa_name %}
     <h1 class="h1">{{ consumer.csa_name }}<small>({{ consumer.csa_identification }})</small></h1>
     </div>
     {% endif %}
    {% endif %}{% endblock %}

     

    It's extending this web template ("Page Web Form Template").

    <div class="wrapper-body">
     <div class="container">
     {% block page-heading %}
     <div class="page-heading">
     {% block breadcrumbs %}
     {% include 'Breadcrumbs' %}
     {% endblock %}
     {% block title %}
     {% include 'Page Header' %}
     {% endblock %}
     </div>
     {% endblock %}
     </div>
     {% block main %}
     <div class="container">
     <div class="row">
     {% include 'Page Copy' %}
     </div>
     {% if page.adx_webform %}
     <div class="row">
     <div class="container">
     {% webform id: page.adx_webform.id %}
     </div>
     </div>
     {% endif %}
     {% endblock %}
     </div>
     <div class="push"></div>
    </div>

      

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

    Create new a Page Template with the Type = rewrite and set the url to Web Form, and try it (note: some Portal types will have Web Form already like Customer Self-Service Portal - in which case you can just set it on the Web Page).

    Not all functionality is available in all templates (not well documented).

  • Ryan S Newcomb Profile Picture
    236 on at

    I did change to the Rewrite and it worked. 

    So, there doesn't appear to be a way to use the validation function recommended on the docs unless using the Rewrite on the Page Template.  In other words, custom validation functions do not work if you have a Web Form on a Web Page that uses a Page Template of type Web Template.

     

    Are there any suggestions to achieving the desired goal here without having to use the Rewrite page template?  We are hoping to keep our custom Web Template (which uses our custom breadcrumbs).

  • OOlashyn Profile Picture
    3,496 Most Valuable Professional on at

    Hmm... Interesting. Ideally, it should work for the custom template as well. @justinburch can you confirm that this is proper behaviour and not a bug?

  • Verified answer
    justinburch Profile Picture
    Microsoft Employee on at

    Hi @OOlashyn, @NewcombR,

    I don't work with the Portal team, but this is an issue I've been seeing since last year prior to joining Microsoft.

    The issue is (as identified) that whatever code is attempting to initialize the webFormClientValidate function isn't working properly, though webFormClientValidate is still being checked (as expected) in the client submission function.

    I tried to find a previous solution I provided that aligned closely with the doc provided sample, but I couldn't find it. The safest solution is to just remove those checks - it would then work for your Web Template.

     

    // your original
    if (window.jQuery) {
     (function ($) {
     if (typeof webFormClientValidate != "undefined") {
     var originalValidationFunction = webFormClientValidate;
     if (
     originalValidationFunction &&
     typeof originalValidationFunction == "function"
     ) {
     webFormClientValidate = function () {
     originalValidationFunction.apply(this, arguments);
     // do your custom validation here
     const tableBody = document.querySelector(
     ".view-grid > table > tbody"
     );
    
     // the tbody element isn't created if there are no records.
     if (!tableBody) {
     alert("You must add at least one requested service.");
     // return false; // to prevent the form submit you need to return false
     return false;
     }
     // end custom validation.
     return true;
     };
     }
     }
     })(window.jQuery);
    }
    
    // simplified
    
    $(document).ready(function () {
     window.webFormClientValidate = function () {
     // do your custom validation here
     const tableBody = document.querySelector(
     ".view-grid > table > tbody"
     );
    
     // the tbody element isn't created if there are no records.
     if (!tableBody) {
     alert("You must add at least one requested service.");
     // return false; // to prevent the form submit you need to return false
     return false;
     }
     // end custom validation.
     return true;
     };
    });

     

     I do intend on seeing if there are internal channels to make this issue known.

    I hope this helps in the meantime.

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