3

I'm using a Kendo Grid / Custom validator editing to validate the a Column in the grid,Actually I'm trying the check the email already exists in the database or not ? to implement it I would like to get ID for the Row.

For example given in reference its products table, so in this case I would to get the ProductID inside the validation function ?

Reference: http://demos.telerik.com/kendo-ui/grid/editing-custom-validation

Binny
  • 328
  • 5
  • 21

2 Answers2

3

You can get the id by retrieving the uid and then getting the data item from the dataSource via dataSource.getByUid(). Each row in the grid has a unique uid generated by the grid.

So for instance, referring to kendo's demo, the validation would now look like this:

productnamevalidation: function (input) {
    //get row and uid
    var row = input.closest('tr')[0];
    var uid = $(row).attr('data-uid');

    //get data item and then its ProductID
    var dataitem = dataSource.getByUid(uid);
    console.log(dataitem);
    console.log(dataitem.ProductID);

    //continue doing validation
    if (input.is("[name='ProductName']") && input.val() != "") {
        input.attr("data-productnamevalidation-msg", "Product Name should start with capital letter");
        return /^[A-Z]/.test(input.val());
    }

    return true;
}

Here is their demo with this code included, you can open the console to see that each data row is being printed out with all its model properties.

gitsitgo
  • 6,589
  • 3
  • 33
  • 45
  • Not sure but the validation event is firing twice now. Any ideas? – Binny Apr 22 '15 at 11:33
  • @Binny Without seeing your code, I can't be sure, but I would say it is [this issue](http://stackoverflow.com/a/24678833/1860561). Basically every validation function runs globally for all columns, even though you only specify it for one column. So make sure your column validation logic is placed in the `if` statement that checks the input box. So for instance in my answer, move all the row ID checking into `if (input.is("[name='ProductName']") && input.val() != "")` – gitsitgo Apr 22 '15 at 14:16
  • [link](http://stackoverflow.com/questions/29796437/kendo-ui-grid-custom-validation-is-firing-twice). Using ajax to validate the email? – Binny Apr 22 '15 at 14:37
  • 1
    FYI, this only works for inline grid editing. If you're using the popup option then your row will be undefined. – ChillyPenguin Dec 28 '15 at 22:47
2

You can get the record's ID with this:

input[0].kendoBindingTarget.source.ID

For example:

emailUnique: function (input) {
    if (input.is("[name=Email]") && input.val() !== "") {
        input.attr("data-emailUnique-msg", "Email already exists");
        return isEmailUnique(input.val(), input[0].kendoBindingTarget.source.ID);
    }
    return true;
}

Bonus track, in case it's useful for someone:

function isEmailUnique(val, id) {
    var data = YourGridDataSource; // If you don't have it, you may need something like $("#YourGrid").data().kendoGrid.dataSource
    for (var i = 0; i < data.length; i++) {
        if (data[i].ID != id && data[i].Email == val)
            return false;
    }
    return true;
}
Andrew
  • 7,602
  • 2
  • 34
  • 42