2

I have a website developed within ASP.NET Core MVC (.NET6) + jQuery, Bootstrap. Due to some restrictions, some navigation pages must be embedded dynamically by non-typical way using Ajax request.

$.ajax({
    url: link,
    type: 'GET',
    dataType: 'html',
    headers: { 'X-Page-Request': 'true' },
    success: function (data) {
        var isFullPage = /<!DOCTYPE *html>/i.test(data);
        if (!isFullPage) {
            $("#contentContainer").empty().append($(data));
        }
    }
});

Some endpoints return partial views with already rendered HTML and JS code and this code embeds into pages after Ajax request using $("#contentContainer").empty().append($(data));

I need to validate the form and show validation outlines only after click on "Create" button, but validation is triggered even after a click on a bootstrap select element or "Cancel" button. I've already set all necessary options to the validator as default.

The thing is that if the code is originally on the page and doesn't integrate dynamically, form validation works as designed. But if I embed the same code using .append() and $.validator.unobtrusive.parse(), I have unexpected form validation after bootstrap select click or any button click. Descried issues appear only in case when rendered HTML and JS code was added dynamically using .append(). As a result, elements with .btn class trigger form validation: all buttons, bootstrap select element etc. I need to prohibit this unexpected form validation and validate only using.valid().

Does anybody have any thoughts how to fix that? Thanks in advance! P.S. I guess it's somehow related to the button element or .btn class.

Embedded code doesn't contain any special things except $.validator.unobtrusive.parse() and looks as usual.

<div id="userModal" class="modal fade" role="dialog" style="display: none;">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="cl-modal-body modal-body">
                <form class="form" id="user1Form">
                    <div class="row shift">
                        <div class="col-xs-12">
                            <div class="flex-align-center">
                                <label for="name" class="input-name-text-align-right input-name">* Name:</label>
                                <input for="name" class="form-control" id="userName" name="name" data-val="true" data-val-required="* This field is required." />
                            </div>
                        </div>
                    </div>
                    <div class="row shift">
                        <div class="col-xs-12">
                            <div class="flex-align-center">
                                <label for="countryId" class="input-name-text-align-right input-name">* Country:</label>
                                <select for="countryId" class="cl-select-form-control form-control" name="countryId" id="countryId1" data-val="true" data-val-required="* This field is required."></select>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
            <div class="cl-modal-footer">
                <button type="button" class="btn cl-modal-button" data-dismiss="modal">Cancel</button>
                <button type="button" id="userModalButton" class="btn btn-primary cl-modal-button">Save</button>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    $(document).ready(function () {
        $.validator.unobtrusive.parse("#userForm");
        initCountries();
        openUserModal();
    });
    ...
    $('#userModalButton').click(function (e) {
        if ($("#userForm").valid()) {
            ...
        }
    });
    ...
</script>
5
  • 1
    Please post a minimal reproducible example of your attempt, noting input and expected output using the [<>] snippet editor. Add the validator from CDN and remove the ajax code if not relevant Commented Feb 1, 2023 at 11:44
  • I am getting an error in w3org on the for attribute in <input for="name" class="form-control" id="userName" name="name" data-val="true" data-val-required="* This field is required." /> Either it needs to be removed as an artefact from a label or you need to add an m to make it a form attribute Commented Feb 1, 2023 at 11:47
  • Both buttons are type="button", which would not normally trigger validation. However, you created a .click() handler for those buttons which calls the .valid() method. FYI - calling .valid() on the form triggers form validation. And your IDs don't match: user1Form in one place and userForm in another. Commented Feb 1, 2023 at 17:37
  • It's correct, I want to validate form using .valid() with handler I created and only this way. But when I embed HTML + JS code through .append() and apply unobtrusive.parse(), it seems the form validation triggers every click on .btn class. Oh, yes, it was a typo with 'user1Form', sorry. Commented Feb 2, 2023 at 9:26
  • Yes, I see now the click handler is only on the one button. However there is something else that we cannot see if it's triggering on both. The validation plugin will not trigger on a type="button" unless there is something that forces it. I see nothing in the code you posted above that explains validation on click of the cancel button. Commented Feb 2, 2023 at 23:22

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.