简体   繁体   中英

Kendo Grid Validation across fields

I am trying to create some validation rules that validate across different fields.

I would like to be able to have the rule "DateClosed must be later than DateOpen", but when I use my custom validation function, it will only pass in the data for DateClosed. How can I get the dateOpen information into my validation function?

Data Source:

schema: {
        model: {
            id: "SomeId",
            fields: {
                 SomeId: { editable: false, nullable: true },
                 Name: { editable: false, validation: { required: false } },
                 Description: { validation: { required: false } },
                 DateOpen: { type: "date", validation: { required: true } },
                 DateClosed: { type: "date", validation: { required: false, validationMessage: "Date Closed must be after Date Opened", custom: testValidation } },
               }
           }
         }

Validation function:

            function testValidation(element) {
                if (element[0] !== null) {
                    if (element[0].name === "DateClosed") {
                       //Date logic here
                        return false;
                    }
                    else
                        return true;
                }
            }

Found this interesting so here is my two cents(and I suppose you already get rid of this problem, actually):

As you are editing the grid(batch edit) the value selected by user in the moment of the validation isn't saved on the dataSource already, is on the memory referenced on the cell(dirty cell). So, you have to get the value of the other field in the dom. The safer way of doing this is to find what cell you want by knowing the column index. Hard-code never is a good idea, ever. So...

var testValidation = function(element) {
    var columnIndex = 0;
    var grid = $("#grid").data("kendoGrid");

    for (columnIndex; columnIndex < grid.columns.length; columnIndex++)
    {
        if (grid.columns[columnIndex].field == "DateOpen")
        {
            break;            
        }
    }

    var dateClosed = $(element).data("kendoDatePicker").value();
    var dateOpen = (new Date($(element).closest("tr").find("td:eq(" + columnIndex + ")").text()));

    return (dateOpen < dateClosed);
}

First it gets the column by a loop, then it's text and evaluates it to a Date object. Once it done, just check if dateOpen < dateClosed .

I hope this helps. Fiddle .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM