kendoValidator如何添加到现有规则功能

时间:2014-07-02 19:58:54

标签: kendo-validator

我希望添加到kendoValidator所需的规则。我希望它以相同的方式运行,但有一个例外。不幸的是,一旦我在规则部分为它提供了一个函数,我就必须再次为所需函数编写所有逻辑。

想知道是否有办法捎带现有的"必需"规则。目前,下面的代码包含所有已标记为禁用的内容,即使其中包含值也是如此。

    function runValidation() {
        $(".dateTimePickerField").each(function () {
            var validator = $(this).kendoValidator({
                rules: {
                    required: function (e) {
                        if ($(e).is(':disabled'))
                        {
                            return true;
                        }
                    },
                    dateValidation: function (e) {
                        var dateTime = $(e).val();
                        var currentDate = Date.parse($(e).val());
                        if (dateTime.length > 0 && !currentDate) {
                            return false;
                        }
                        return true;
                    }
                },
                messages: {
                    //Define your custom validation massages
                    required: "datetime required",
                    dateValidation: "Invalid datetime"
                }
            }).data("kendoValidator");

            validator.validate();
        });
    }

1 个答案:

答案 0 :(得分:1)

我刚刚编写了所需规则的必要逻辑。编码规则并不难,所以我就这么做了。

   function runValidation() {
        var boolval = true;
        $("input.dateTimePickerField").each(function () {
            var validator = $(this).kendoValidator({
                rules: {
                    required: function (e) {
                        if ($(e).prop('required')) {
                            if ($(e).is(':disabled') || $(e).val().length > 0) {
                                return true;
                            }
                            return false;
                        }
                        return true;
                    },
                    dateValidation: function (e) {
                        var dateTime = $(e).val();
                        var currentDate = Date.parse($(e).val());
                        if (dateTime.length > 0 && !currentDate) {
                            return false;
                        }
                        return true;
                    },
                    customEventDateValidation: function (e) {
                        var dateTime = $(e).val();
                        var currentDate = Date.parse($(e).val());

                        var row = $(e).closest('tr');
                        var startDateText = row.find(".startDate").text();
                        var eventStartDate = Date.parse(startDateText);

                        if (currentDate && eventStartDate)
                        {
                            if(currentDate > eventStartDate)
                            {
                                return false;
                            }
                        }
                        return true;
                    },
                    customTicketStartDateValidation: function (e) {
                        if ($(e).hasClass("ticketStartDate"))
                        {
                            var dateTime = $(e).val();
                            var currentDate = Date.parse($(e).val());
                            var row = $(e).closest('tr');
                            var ticketEndDateText = row.find("input.ticketEndDate").val();
                            var ticketEndDate = Date.parse(ticketEndDateText);
                            if(currentDate && ticketEndDate)
                            {
                                if(currentDate > ticketEndDate)
                                {
                                    return false;
                                }
                            }
                        }
                        return true;
                    },
                    customTicketEndDateValidation: function (e) {
                        if ($(e).hasClass("ticketEndDate")) {
                            var dateTime = $(e).val();
                            var currentDate = Date.parse($(e).val());
                            var row = $(e).closest('tr');
                            var ticketStartDateText = row.find("input.ticketStartDate").val();
                            var ticketStartDate = Date.parse(ticketStartDateText);
                            if (currentDate && ticketStartDate) {
                                if (currentDate < ticketStartDate) {
                                    return false;
                                }
                            }
                        }
                        return true;
                    }
                },
                messages: {
                    required: "Datetime required",
                    dateValidation: "Invalid datetime",
                    customEventDateValidation: "Datetime must be before event date",
                    customTicketStartDateValidation: "Ticket start datetime must be before ticket end datetime",
                    customTicketEndDateValidation: "Ticket end datetime must be after ticket start datetime"
                }
            }).data("kendoValidator");

            if (!validator.validate()) {
                boolval = false;
            }
        });
        return boolval;
    }