I have many int32 entity properties. Binding to input fields they are converted to string when modified so the validation fails.
Browsing the web I found that this is a problem using ko.
The first solution I found was implement a ko extenders (copied from thei site):
ko.extenders.numeric = function (target, precision) { ....
and apply to all numeric fields before save:
function salva() {
if (em.hasChanges()) {
oModelRuntime.RIGHE_VISIBILI_AP.extend({ numeric: 0 });
var isValid = oModelRuntime.entityAspect.validateProperty("RIGHE_VISIBILI_AP");
alert(isValid);
em.saveChanges()
.then(function (saveResult) {
But I have hundreds of fields to apply the extender.
The second, and for me best, solution was to modify your source:
function intRangeValidatorCtor(validatorName, minValue, maxValue) {
ctor.messageTemplates[validatorName] = core.formatString("'%displayName%' must be an integer between the values of %1 and %2",
minValue, maxValue);
return function () {
var valFn = function (v, ctx) {
if (v == null) return true;
if (typeof v === "string") // added
v = parseInt(v, 0);
if ((typeof v === "number") && Math.floor(v) === v) {
if (minValue != null && v < minValue) {
return false;
}
if (maxValue != null && v > maxValue) {
return false;
}
return true;
} else {
return false;
}
};
return new ctor(validatorName, valFn);
};
Let me know if this is a correct approach except more refinements. If the property type is int32 (number) for me is logical that the validator try to convert if the input value is a string.