Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
288 views
in Technique[技术] by (71.8m points)

javascript - Kendo跨字段验证(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.

(我希望规则“ DateClosed必须晚于DateOpen”,但是当我使用自定义验证功能时,它将仅传递DateClosed的数据。)

How can I get the dateOpen information into my validation function?

(如何将dateOpen信息放入验证功能?)

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;
                }
            }
  ask by GodsCrimeScene translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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).

(在编辑网格(批处理编辑)时,用户在验证时选择的值尚未保存在dataSource上,而是存储在单元格(脏单元格)上引用的内存中。)

So, you have to get the value of the other field in the dom.

(因此,您必须获取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.

(首先,它通过循环获取列,然后是文本,并将其求值为Date对象。)

Once it done, just check if dateOpen < dateClosed .

(完成后,只需检查dateOpen < dateClosed 。)

I hope this helps.

(我希望这有帮助。)

Fiddle .

(小提琴 。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...