Kendo Validator Not Blocking Form Submission

1 Answer 554 Views
TextBox
Brian
Top achievements
Rank 2
Iron
Brian asked on 22 Feb 2023, 03:21 PM

Hi,

I have a Kendo Validator with some custom rules, including hitting an API to prevent name duplication.


            rules: {
                noDups: function (input: any) {
                    if (input[0].id == "Name") {

                        let url = $("#duplicate-link").data("url") + "?name=" + input.val();

                        $.get(url, function (response) {
                            return !response;
                        });
                    }

                    return true;
                },

Basically, if the name comes back as "already found" from the API, return false.

I have logged the output and the response is returning false when the name matches.

However, when validating the form, only the required validator is firing.  I don't see the false return value affecting the validator's red box and validation message, and the form still submits.


        $("#submit").on("click", function () {
            let validator = $("#form1").data("kendoValidator");

            if (validator.validate()) {
                console.log('validated');
            }
        });

                @(Html.Kendo().TextBoxFor(m => m.Name).HtmlAttributes(new { @class = "form-control", placeholder = "Application Name", required = true, data_noDups_msg = "This field is required" }))

Any thoughts?  Thanks!

1 Answer, 1 is accepted

Sort by
0
Aleksandar
Telerik team
answered on 27 Feb 2023, 07:17 AM

Hi Brian,

We have the following knowledgebase article that demonstrates how to implement remote validation for the Kendo Validator. The example is for ASP.NET MVC and uses a Grid, though the approach would be the same for ASP.NET Core. You can review a runnable sample in this Demo - enter an existing ProductName and the validation will fire.

I hope this helps.

Regards,
Aleksandar
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Brian
Top achievements
Rank 2
Iron
commented on 27 Feb 2023, 12:15 PM

Hi Aleksandar,

Unfortunately, this doesn't answer my question.  All of my other validation is working except for this one field, and the remote validation is correctly returning a true/false when valid/invalid.  It just isn't blocking the form submission or highlighting the field red when it fails validation.

Thanks!

Brian
Top achievements
Rank 2
Iron
commented on 27 Feb 2023, 03:50 PM

The debug is saying the control is validated when the custom rule has failed.  I'm seeing the red box around the field, the error message is displaying, but the form is still posting.


        let validator = $("#form1").kendoValidator({
            messages: {
                remote: "This name already exists",
            },

            rules: {
                remote: function (input: any) {
                    if (input.val() == "" || !input.attr("data-val-remote-url")) {
                        return true;
                    }

                    if (input.attr("data-val-remote-recieved")) {
                        input.attr("data-val-remote-recieved", "");

                        if (input.attr("data-val-remote") == false) {
                            input.addClass("is-invalid");
                        } else {
                            input.removeClass("is-invalid");
                        }

                        return (input.attr("data-val-remote"));
                    }

                    let validator = this;
                    var currentInput = input;

                    if (input[0].id == "Name") {

                        let url = $("#duplicate-link").data("url") + "?name=" + input.val();

                        $.get(url, function (response) {
                            if (response == true) {
                                input.attr("data-val-remote", "");
                            }
                            else {
                                input.attr("data-val-remote", response);
                            }
                            input.attr("data-val-remote-recieved", true);
                            validator.validateInput(currentInput);
                        });
                    }

                    return true;
                }
            }
        }).data("kendoValidator");

        $("#submit").on("click", function (e) {
            if (validator.validate()) {
                console.log('validated');
            }
            else {
                console.log('not validated');
            }
        });

I did have to manually highlight the field red, maybe that's part of it?  But the custom rule is returning the correct value and is displaying the error message as normal.

Thanks!

Nikolay
Telerik team
commented on 02 Mar 2023, 11:03 AM

Hi Brian,

This is a bug and I have logged it for fixing. Please refer to the below links:

As a token of gratitude, I have added some Telerik Points to your account.

regards,

Nikolay

Tags
TextBox
Asked by
Brian
Top achievements
Rank 2
Iron
Answers by
Aleksandar
Telerik team
Share this question
or