Custom rules are not working in kendo validation

2 posts, 0 answers
  1. Lopamudra
    Lopamudra avatar
    1 posts
    Member since:
    Jan 2015

    Posted 23 Sep 2015 Link to this post

    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;
                        }
                    }
                });
            });

  2. Rosen
    Admin
    Rosen avatar
    3234 posts

    Posted 25 Sep 2015 Link to this post

    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!
     
  3. Kendo UI is VS 2017 Ready
Back to Top