This is a migrated thread and some comments may be shown as answers.

Different set of custom validation for gridview add new and update

1 Answer 66 Views
Validation
This is a migrated thread and some comments may be shown as answers.
lucerias
Top achievements
Rank 1
lucerias asked on 19 May 2016, 04:58 PM

I have currently added the following custom validation to my interest name, but interest name has to be unique, so when user add new and existing interest name is already there, it will throw error. However, when user try to edit the record, it should be able to detect whether current interest name is changed, if it is a new interset name then only check only uniqueness validation, otherwise, it should allow the update to go through successfully.

With the following validation, when i try to perform update, it also throw the validation error, and i am do not know how to make this validation valid for Add New all the time, and valid for Update only when interest name is changed.

    (function ($, kendo) {
        $.extend(true, kendo.ui.validator, {
            rules: { // custom rules
                InterestNamevalidation: function (input, params) {
                    if (input.is("[name='InterestName']") && input.val() != "") {
                        var dataSource = $("#Customer").data("kendoGrid").dataSource;
                        var data = dataSource.data();

                        for (var i = 1; i < data.length; i++) {
                            if ($.trim(input.val().toLowerCase()) == $.trim(data[i].InterestName.toLowerCase())) {
                                input.attr("data-InterestNamevalidation-msg", "InterestName should be unique");
                                return false;

                            }
                        }
                    }
                    return true;
                }
            }
        });
    })(jQuery, kendo);

1 Answer, 1 is accepted

Sort by
0
Georgi Krustev
Telerik team
answered on 23 May 2016, 02:41 PM
Hello lucerias,

I would suggest you enhance your validation condition and check whether the model is new. When it is new, you are adding new item and you can continue with the check. If it is not new, then it is an Update action and you can skip this custom validation:
if ($.trim(input.val().toLowerCase()) == $.trim(data[i].InterestName.toLowerCase())) {
if (data[i].isNew()) { return true; }
//it is new and we need to throw the error
input.attr("data-InterestNamevalidation-msg", "InterestName should be unique");
return false;
}

Regards,
Georgi Krustev
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
Tags
Validation
Asked by
lucerias
Top achievements
Rank 1
Answers by
Georgi Krustev
Telerik team
Share this question
or