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

Custom rules are not working in kendo validation

1 Answer 476 Views
Validation
This is a migrated thread and some comments may be shown as answers.
Lopamudra
Top achievements
Rank 1
Lopamudra asked on 23 Sep 2015, 05:50 AM

Hello,

 

I am trying to implement custom rules for validator, where we need to make certain input fields mandatory only when a check box is selected. But not able to get it to work, can you please point out if i missed out anything in below -

 

 

var validator = $("#form1").kendoValidator({
            errorTemplate: "<span></span>",
            validate: function (e) {
                var html = "";
                e.preventDefault();
                if (!e.valid) {
                    var errors = this.errors();
                    var requiredFieldCount = 0;
                    html += "<ul>";
                    for (var i = 0; i < errors.length; i++) {
                        if (errors[i].toLowerCase().indexOf("required") > 0 && requiredFieldCount == 0) {
                            html += "<li> Please enter mandatory Fields *</li>";
                            requiredFieldCount++;
                        }
                        else if (errors[i].toLowerCase().indexOf("required") < 0) {
                            html += "<li>" + errors[i] + "</li>";
                        }
                    }
                    html += "</ul>";
                }
                $("#errors").html($(html));
            }
        }).data("kendoValidator");

 

 

 

    

Custom Rule - 
 
$(function () {
            var container = $("#form1");
            kendo.init(container);
            container.kendoValidator({
                rules: {
                    InsuranceAmount: function (input) {
                        if (input.context.id == "" && input.val() != "") {
                            return $("#.HasInsurance:checked");                          
                        }
 
                        return true;
                    }
                }
            });
        });

1 Answer, 1 is accepted

Sort by
0
Rosen
Telerik team
answered on 25 Sep 2015, 07:23 AM

Hello Lopamudra,

I suspect that the issue is caused by incorrect configuration. There are multiple Validator instances created in the snippets you have pasted - each snippet creates a separate Kendo UI Validator by calling $().kendoValidator({/*..*/}). Also note that the rules should be set to the widget at the point of  its declaration it is not supported to attach them after the fact.

Regards,
Rosen
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
Lopamudra
Top achievements
Rank 1
Answers by
Rosen
Telerik team
Share this question
or