Reputation: 4094
I have a kendo data grid as shown below:
$("#Grid").kendoGrid({
save: function (data) {
if (data.values.UnitPrice !== undefined) {
if(data.values.UnitPrice == null || data.values.UnitPrice == 0){
data.model.set("EstimatedItemCost", 0);
}
else{
console.log(data.values.UnitPrice * data.model.Quantity);
var test = data.model.set("EstimatedItemCost", data.values.UnitPrice * data.model.Quantity);
}
}
this.refresh();
},
....//other config
and it's using data source with a little custom validation
var tdata = new kendo.data.DataSource({
schema: {
model: {
fields: {
UnitPrice: {
type: "number",
validation:{
min: 0,
conditionalRequired: function(input){
if(input.is("[name='UnitPrice']")){
input.attr("data-conditionalRequired-msg", "Unit Price is required");
//Some logic, without changing the result, simply return true
return true;
}
}
},
},
EstimatedItemCost: { type: "number" },
Remarks: { type: "string" }
}
}
},
Okay, Now here is the description of the problem:
data.model.set("EstimatedItemCost", data.values.UnitPrice * data.model.Quantity);
does not set the value of EstimatedItemCost
, it remains 0 anyway.
i.e.: console.log(data.values.UnitPrice * data.model.Quantity);
has show a valid number like 200, but the value of EstimatedItemCost
is still 0 after the set()
.
With further drilling, I found it is due to the existence of the custom validation in the datasource
conditionalRequired
, after I remove it, everything goes fine!
But I really need the validation logic, while setting the value of model explicitly.
Can anyone tell me what is the cause of this problem, and how can I solve it?
Any help is appreciated! Thanks!
EDIT1:
Adding a few console log, I found that after the set()
, the custom validation function runs once again! (and fail), I don't know what trigger the validation once again though...
Upvotes: 0
Views: 1068
Reputation: 1978
your validation is wrong , it should look like this
var tdata = new kendo.data.DataSource({
schema: {
model: {
fields: {
UnitPrice: {
type: "number",
validation:{
min: 0,
conditionalRequired: function(input){
if(input.is("[name='UnitPrice']")){
input.attr("data-conditionalRequired-msg", "Unit Price is required");
//Some logic, without changing the result, simply return true
return true;
}else{
return false;
}
return true;
}
},
},
EstimatedItemCost: { type: "number" },
Remarks: { type: "string" }
}
}
},
basically the conditionalRequired will run for all your model items , so u should return true if(input.is("[name='UnitPrice']")) not what you need to validate.
Upvotes: 1