0

我正在寻找添加到 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();
        });
    }
4

1 回答 1

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;
    }
于 2014-07-03T20:27:01.847 回答