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

saveChanges from edit event validation

6 Answers 247 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Wit
Top achievements
Rank 1
Wit asked on 15 Oct 2014, 03:51 PM
Hi,
I am trying to call saveChanges from edit event
function gridEdit(e) {
             var fieldName = e.container.find("input[name]").attr("name");
 
 
             if (fieldName) {
                 //new row validtaion
                 if (e.model.isNew() && fieldName == "EntityName") {
 
                     var validator = e.container.getKendoValidator();
                     var options = validator.options;
 
                     options.rules.testRule = function (input) {
                         if (input.is("[name='EntityName']") && input.val() != "") {
 
                             $scope.kgrid.saveChanges();
 
                              
                         }
                         return true;
                     }
                      
 
                         options.messages.testRule = "is not unique.";
                         validator.setOptions(options);
                      
 
                 }
             }
         }


It seems to create a loop. Any suggestions how I could safely call saveChanges() ? Sample project

6 Answers, 1 is accepted

Sort by
0
Accepted
Alexander Popov
Telerik team
answered on 17 Oct 2014, 02:16 PM
Hi Wit,

Calling the saveChanges method with a timeout should prevent the infinite loop. For example: 
if (input.is("[name='EntityName']") && input.val() != "") {
    setTimeout(function () {
        $scope.kgrid.saveChanges();
    });
}


Regards,
Alexander Popov
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Wit
Top achievements
Rank 1
answered on 17 Oct 2014, 02:29 PM
Hi Alexander,

That looks great, the only problem is that  my grid edit event is fired twice so I have two records created. Is that expected behaviour? Is there any workaround for it?

Thank you
0
Alexander Popov
Telerik team
answered on 21 Oct 2014, 11:55 AM
Hi Wit,

Generally speaking, manipulating data from withing the validation rules is not considered a good design. The validation should either permit or deny certain actions, not take actions itself, so I would suggest returning "false" if certain conditions are not met. This would allow the users to save the data as usual, but it will prevent them from doing so if the data is not matching the validation rules.

Regards,
Alexander Popov
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Wit
Top achievements
Rank 1
answered on 21 Oct 2014, 12:30 PM
Hi Alexander,

I agree with you, but my requirement is to auto save the grid then certain conditions are meet. I would appreciate if  you could recommend other way of doing it.

Thank you
0
Alexander Popov
Telerik team
answered on 23 Oct 2014, 08:32 AM
Hello again Wit,

Closing the cell and calling the saveChanges method work as expected on my side, as seen in this screencast. Keep in mind that if the Create request did not succeed it will be sent again with the next call of the saveChanges method.

Regards,
Alexander Popov
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Wit
Top achievements
Rank 1
answered on 23 Oct 2014, 09:18 AM
Thank you
Tags
Grid
Asked by
Wit
Top achievements
Rank 1
Answers by
Alexander Popov
Telerik team
Wit
Top achievements
Rank 1
Share this question
or