Skip to main content

Notifications

Community site session details

Community site session details

Session Id : AH6xq5ZGx6C17asCvHL1ja
Power Pages - Power Apps Portals
Answered

Web Form Step - Prevent Submit

Like (0) ShareShare
ReportReport
Posted on 15 Mar 2021 18:11:16 by 234

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

 

  • justinburch Profile Picture
    Microsoft Employee on 17 Mar 2021 at 01:47:48
    Re: Web Form Step - Prevent Submit

    Hi @Fubar,

    Agreed, I think it’s a bug but Microsoft changes. I remember this functionality working (and debugging it end to end) when first learning Dynamics Portals, which is how I knew where to look to resolve it the first time. It would definitely have been after ADX’s versions, and would’ve likely been after 9.0+.

     

    I hadn’t noticed which portals install the different aspx pages, but it makes sense - some of the portals added in plugins to create “APIs” before that was released as an actual feature, and some come with Custom Workflow Activities that can be used as steps in workflows - eg the Partner Portal comes with a “For Each” step that allows you to run a workflow against the N relationships of the target record.


    I’ve linked this thread in an internal communication, so hopefully there’s some traction and your analysis on the aspx-or-Web Template finding can help lead to the source KT the bug!

  • justinburch Profile Picture
    Microsoft Employee on 17 Mar 2021 at 01:42:22
    Re: Web Form Step - Prevent Submit

    Hi @NewcombR,


    Correct, if Page_Validators is missing then it’ll also skip the inner block. The same issue can occur for custom validations (such as code snippets to add “dynamic validation” - eg, “field a is required only if field b is value X”), where people share that code sample and don’t realize that Page_Validators might not be initiated.

     

    Typically, if your form has 0 required fields or enforced validations, then Page_Validators will be missing. Examples of this are basic Single Line of Text fields in which none are required. Usually these don’t use any other Validators, as things like character limits are handled by attributes on the input itself.

     

    When this occurs, you’d similarly have to create the Page_Validators object, but creating the validations themselves are typically not handled well in the usual code snippets, but it also isn’t usually a problem either.

  • Fubar Profile Picture
    7,850 Super User 2025 Season 1 on 17 Mar 2021 at 01:31:31
    Re: Web Form Step - Prevent Submit

    @NewcombR  only if Page_Validators is not there - the reason why they have it return is that later in the code you add/push your custom validator onto that object (and it would error if not in existence).  As far as I know the Page_Validator code is just used for Entity Form validation, and haven't heard or personally seen any issues with it.

  • Fubar Profile Picture
    7,850 Super User 2025 Season 1 on 17 Mar 2021 at 01:25:00
    Re: Web Form Step - Prevent Submit

    @justinburchthe issue appears to be that Microsoft (or Adx) have dropped a piece of JavaScript.

     

    If you look at a Web Page rendered based on the .aspx Page Templates you will see it includes the following script block

    <script type="text/javascript">
    	function entityFormClientValidate() {
    		// Custom client side validation. Method is called by the submit button's onclick event.
    		// Must return true or false. Returning false will prevent the form from submitting.
    		return true;
    	}
    
    	function webFormClientValidate() {
    		// Custom client side validation. Method is called by the next/submit button's onclick event.
    		// Must return true or false. Returning false will prevent the form from submitting.
    		return true;
    	}
    </script>

     

    Where as what is rendered by the Web Template based Page Templates

    <script type="text/javascript">					
    	function entityFormClientValidate() {
    		// Custom client side validation. Method is called by the submit button's onclick event.
    		// Must return true or false. Returning false will prevent the form from submitting.
    		return true;
    	}			
    </script>

    As its not defined in the above the Custom Validator code returns 'undefined' rather than 'function'

     

    What I have also noticed is that some Page Templates are rewrite (.aspx based) for one Portal install vs another type of Portal install  e.g. Full Page (Page Template) is .aspx in Partner Portal and Web Template in Blank/Custom Portal (still has the .aspx populated on the Page Template record - but is using Web Template).  

  • Ryan S Newcomb Profile Picture
    234 on 16 Mar 2021 at 19:06:16
    Re: Web Form Step - Prevent Submit

    @justinburch - Would the same apply to the additional client side validation function as described here?

     

    This line from the documented code in the docs seems as if it would cause the function to terminate prior to adding the custom validator.

    if (typeof (Page_Validators) == 'undefined') return;

     

  • Ryan S Newcomb Profile Picture
    234 on 16 Mar 2021 at 18:02:58
    Re: Web Form Step - Prevent Submit

    Thank you, @justinburch!  This worked perfectly.

  • Verified answer
    justinburch Profile Picture
    Microsoft Employee on 16 Mar 2021 at 17:52:42
    Re: Web Form Step - Prevent Submit

    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.

  • OOlashyn Profile Picture
    3,496 Most Valuable Professional on 16 Mar 2021 at 17:39:35
    Re: Web Form Step - Prevent Submit

    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?

  • Ryan S Newcomb Profile Picture
    234 on 16 Mar 2021 at 17:36:26
    Re: Web Form Step - Prevent Submit

    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).

  • Fubar Profile Picture
    7,850 Super User 2025 Season 1 on 16 Mar 2021 at 04:19:16
    Re: Web Form Step - Prevent Submit

    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).

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

#1
WarrenBelz Profile Picture

WarrenBelz 146,745 Most Valuable Professional

#2
RandyHayes Profile Picture

RandyHayes 76,287 Super User 2024 Season 1

#3
Pstork1 Profile Picture

Pstork1 66,091 Most Valuable Professional

Leaderboard
Loading started
Loading complete