cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
NewcombR
Advocate III
Advocate III

Web Form Step - Prevent Submit

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
justinburch
Microsoft
Microsoft

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.

View solution in original post

16 REPLIES 16
OOlashyn
Super User
Super User

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.

----------------------------------------------------
If you find this post helpful consider marking it as a solution to help others find it.
NewcombR
Advocate III
Advocate III

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?

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?

 

----------------------------------------------------
If you find this post helpful consider marking it as a solution to help others find it.

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
Solution Sage
Solution Sage

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

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
Solution Sage
Solution Sage

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

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

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?

----------------------------------------------------
If you find this post helpful consider marking it as a solution to help others find it.

Helpful resources

Announcements
PA User Group

Welcome to the User Group Public Preview

Check out new user group experience and if you are a leader please create your group

secondImage

Demo Extravaganza is Back!

We are excited to announce that Demo Extravaganza for 2021 has started!

MBAS on Demand

Microsoft Business Applications Summit sessions

On-demand access to all the great content presented by the product teams and community members! #MSBizAppsSummit #CommunityRocks

Power Apps June 2021

June Power Apps Community Call

Did you miss the call? Check out the recording here!

Users online (38,561)