This is a migrated thread and some comments may be shown as answers.

Can't find validator object for custom validator

1 Answer 322 Views
Validation
This is a migrated thread and some comments may be shown as answers.
Jake
Top achievements
Rank 1
Jake asked on 07 May 2015, 04:21 AM

We have a kendo Grid, and then we have an editor template, which is named after the view model.

Inside the viewModel we have this:

 

[Required( ErrorMessage = "Please enter a name")]

public string Name{ get; set; }

 

Then inside the javascript, I create another custom rule like this :

                var form = $("#AdhocGroupForm")

                form.kendoValidator({
                                messages: {
                                                nameexists: "This name already exists, please choose another"
                                },
                                rules: {
                                                nameexists: function(input) {
                                                                if (input.is("[name=Name]")) {
                                                                                 return input.val() === "Tom";
                                                                                }
                                                                return true;
                                                }
                                }
                });

 

Of course, this does nothing right now. I'm simply trying to set this up and get to the point where it's called, then I'll flesh it out to check if the provided name exists, via AJAX. However, it's not called, and that is part of the issue.

The other part is that when I do this:

                var validator = $("#AdhocGroupForm").kendoValidator().data("kendoValidator");

I expect to get an object that I can call 'validate' on ( I assume this is what I need to do to trigger validation although some things I've seen make me think it will fire on each keypress ). However, this returns null. kendoValidator() returns an object, but this has no validate method, either. I believe this is my core issue, I don't have a validator to work with.The form and textbox html in my page looks like this:

 

                <form action="/AdhocGroup/AdhocGroupForm/AdhocGroupForm" data-role="validator" method="post" novalidate="novalidate">
                <input class="input k-textbox" data-val="true" data-val-nameexists="data-val-nameexists" data-val-required="Please enter a name" id="Name" name="Name" style="width: 200px;" type="text" value="" data-bind="value:Name">

The validation for the required field, with my custom error message, works fine.This is my first day using these validators, so it's possible I've missed something obvious. Thanks for any advice.

1 Answer, 1 is accepted

Sort by
0
Alexander Valchev
Telerik team
answered on 08 May 2015, 05:15 PM
Hi Jake,

The following code will re-initialize the validation and reset all the previously configured custom rules and messages.

var validator = $("#AdhocGroupForm").kendoValidator().data("kendoValidator");

Please remove the code marked in red and let me know if the issue still persists.

Regards,
Alexander Valchev
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
Tags
Validation
Asked by
Jake
Top achievements
Rank 1
Answers by
Alexander Valchev
Telerik team
Share this question
or