Skip to content

Commit d2b1c44

Browse files
authored
VCST-410: Incorrect validation for decimal fields in settings (#2757)
fix: Incorrect validation for decimal fields in settings if value is empty string.
1 parent a3b266c commit d2b1c44

File tree

1 file changed

+60
-61
lines changed
  • src/VirtoCommerce.Platform.Web/wwwroot/js/common/directives

1 file changed

+60
-61
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,44 @@
11
'use strict';
22

33
angular.module('platformWebApp')
4-
// TODO: Replace with tested localized version (see below)
5-
.directive('smartFloat', ['$filter', '$compile', 'platformWebApp.userProfile', function ($filter, $compile, userProfile) {
6-
var INTEGER_REGEXP = /^\-?\d+$/; //Integer number
7-
var INTEGER_MAX_VALUE = 2147483647;
8-
var INTEGER_MIN_VALUE = -2147483648;
9-
var FLOAT_REGEXP_1 = /^[-+]?\$?\d{1,3}(\.\d{3})+(,\d*)$/; //Numbers like: 1.234,5678
10-
var FLOAT_REGEXP_2 = /^[-+]?\$?\d{1,3}(,\d{3})+(\.\d*)$/; //Numbers like: 1,234.5678
11-
var FLOAT_REGEXP_3 = /^[-+]?\$?\d+(\.\d*)?$/; //Numbers like: 1234.5678
12-
var FLOAT_REGEXP_4 = /^[-+]?\$?\d+(,\d*)?$/; //Numbers like: 1234,5678
4+
// TODO: Replace with tested localized version (see below)
5+
.directive('smartFloat', ['$filter', '$compile', 'platformWebApp.userProfile', function ($filter, $compile, userProfile) {
6+
var INTEGER_REGEXP = /^\-?\d+$/; //Integer number
7+
var INTEGER_MAX_VALUE = 2147483647;
8+
var INTEGER_MIN_VALUE = -2147483648;
9+
var FLOAT_REGEXP_1 = /^[-+]?\$?\d{1,3}(\.\d{3})+(,\d*)$/; //Numbers like: 1.234,5678
10+
var FLOAT_REGEXP_2 = /^[-+]?\$?\d{1,3}(,\d{3})+(\.\d*)$/; //Numbers like: 1,234.5678
11+
var FLOAT_REGEXP_3 = /^[-+]?\$?\d+(\.\d*)?$/; //Numbers like: 1234.5678
12+
var FLOAT_REGEXP_4 = /^[-+]?\$?\d+(,\d*)?$/; //Numbers like: 1234,5678
1313

14-
return {
15-
require: 'ngModel',
16-
link: function (scope, elm, attrs, ctrl) {
14+
return {
15+
require: 'ngModel',
16+
link: function (scope, elm, attrs, ctrl) {
1717
// possible values for fraction are: 0, positive number, negative number, none
1818
// when fraction is a negative number result has maximum length of the fractional part of the value
19-
var fraction = (attrs.fraction || Number(attrs.fraction) === 0) ? attrs.fraction : 2;
20-
if (attrs.numType === "float") {
21-
ctrl.$parsers.unshift(function (viewValue) {
22-
if (FLOAT_REGEXP_1.test(viewValue)) {
23-
ctrl.$setValidity('float', true);
24-
return parseFloat(viewValue.replace('.', '').replace(',', '.'));
25-
} else if (FLOAT_REGEXP_2.test(viewValue)) {
26-
ctrl.$setValidity('float', true);
27-
return parseFloat(viewValue.replace(',', ''));
28-
} else if (FLOAT_REGEXP_3.test(viewValue)) {
29-
ctrl.$setValidity('float', true);
30-
return parseFloat(viewValue);
31-
} else if (FLOAT_REGEXP_4.test(viewValue)) {
32-
ctrl.$setValidity('float', true);
33-
return parseFloat(viewValue.replace(',', '.'));
34-
} else {
35-
//Allow to use empty values
36-
ctrl.$setValidity('float', !viewValue);
37-
return viewValue;
38-
}
39-
});
19+
var fraction = (attrs.fraction || Number(attrs.fraction) === 0) ? attrs.fraction : 2;
20+
if (attrs.numType === "float") {
21+
ctrl.$parsers.unshift(function (viewValue) {
22+
if (FLOAT_REGEXP_1.test(viewValue)) {
23+
ctrl.$setValidity('float', true);
24+
return parseFloat(viewValue.replace('.', '').replace(',', '.'));
25+
} else if (FLOAT_REGEXP_2.test(viewValue)) {
26+
ctrl.$setValidity('float', true);
27+
return parseFloat(viewValue.replace(',', ''));
28+
} else if (FLOAT_REGEXP_3.test(viewValue)) {
29+
ctrl.$setValidity('float', true);
30+
return parseFloat(viewValue);
31+
} else if (FLOAT_REGEXP_4.test(viewValue)) {
32+
ctrl.$setValidity('float', true);
33+
return parseFloat(viewValue.replace(',', '.'));
34+
} else {
35+
ctrl.$setValidity('float', false);
36+
return viewValue;
37+
}
38+
});
4039

41-
ctrl.$formatters.unshift(
42-
function (modelValue) {
40+
ctrl.$formatters.unshift(
41+
function (modelValue) {
4342
if (modelValue == null) {
4443
return modelValue;
4544
}
@@ -51,29 +50,29 @@ angular.module('platformWebApp')
5150
return new Intl.NumberFormat(userProfile.language || 'default', { maximumFractionDigits: -fraction }).format(resultValue);
5251
}
5352
// default behavior
54-
return $filter('number')(resultValue, fraction);
55-
}
56-
);
57-
}
58-
else if (attrs.numType === "positiveInteger") {
59-
ctrl.$parsers.unshift(function (viewValue) {
60-
ctrl.$setValidity('positiveInteger', INTEGER_REGEXP.test(viewValue) && viewValue > 0);
61-
return viewValue;
62-
});
63-
}
64-
else {
65-
ctrl.$parsers.unshift(function (viewValue) {
66-
if (INTEGER_REGEXP.test(viewValue) && viewValue >= INTEGER_MIN_VALUE && viewValue <= INTEGER_MAX_VALUE) {
67-
ctrl.$setValidity('integer', true);
68-
return viewValue;
69-
}
70-
else {
71-
//Allow to use empty values
72-
ctrl.$setValidity('integer', !viewValue);
73-
return viewValue;
74-
}
75-
});
76-
}
77-
}
78-
};
79-
}]);
53+
return $filter('number')(resultValue, fraction);
54+
}
55+
);
56+
}
57+
else if (attrs.numType === "positiveInteger") {
58+
ctrl.$parsers.unshift(function (viewValue) {
59+
ctrl.$setValidity('positiveInteger', INTEGER_REGEXP.test(viewValue) && viewValue > 0);
60+
return viewValue;
61+
});
62+
}
63+
else {
64+
ctrl.$parsers.unshift(function (viewValue) {
65+
if (INTEGER_REGEXP.test(viewValue) && viewValue >= INTEGER_MIN_VALUE && viewValue <= INTEGER_MAX_VALUE) {
66+
ctrl.$setValidity('integer', true);
67+
return viewValue;
68+
}
69+
else {
70+
//Allow to use empty values
71+
ctrl.$setValidity('integer', !viewValue);
72+
return viewValue;
73+
}
74+
});
75+
}
76+
}
77+
};
78+
}]);

0 commit comments

Comments
 (0)