[FIX] web: integer not properly parsed
The parsing of the integers does not escape the thousands separator. issue: Integers are currently not properly parsed. For example, create a database in Spanish language, install product_expiry and in settings activate Lots & Serial Numbers. Go to Products and create a new product. Choose to track by lots and specify the use_time, life_time, etc. When saving, all integers are set to 0
This commit is contained in:
committed by
Christophe Matthieu
parent
2f3b469dee
commit
b8dac2ff32
@@ -421,13 +421,36 @@ function parseDateTime(value, field, options) {
|
||||
throw new Error(_.str.sprintf(core._t("'%s' is not a correct datetime"), value));
|
||||
}
|
||||
|
||||
function parseFloat(value) {
|
||||
/**
|
||||
* Parse a String containing number in language formating
|
||||
*
|
||||
* @param {string} value
|
||||
* The string to be parsed with the setting of thousands and
|
||||
* decimal separator
|
||||
* @returns {float|NaN} the number value contained in the string representation
|
||||
*/
|
||||
function parseNumber(value) {
|
||||
if (core._t.database.parameters.thousands_sep) {
|
||||
var escapedSep = _.str.escapeRegExp(core._t.database.parameters.thousands_sep);
|
||||
value = value.replace(new RegExp(escapedSep, 'g'), '');
|
||||
}
|
||||
value = value.replace(core._t.database.parameters.decimal_point, '.');
|
||||
var parsed = Number(value);
|
||||
if (core._t.database.parameters.decimal_point) {
|
||||
value = value.replace(core._t.database.parameters.decimal_point, '.');
|
||||
}
|
||||
return Number(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a String containing float in language formating
|
||||
*
|
||||
* @param {string} value
|
||||
* The string to be parsed with the setting of thousands and
|
||||
* decimal separator
|
||||
* @returns {float}
|
||||
* @throws {Error} if no float is found respecting the language configuration
|
||||
*/
|
||||
function parseFloat(value) {
|
||||
var parsed = parseNumber(value);
|
||||
if (isNaN(parsed)) {
|
||||
throw new Error(_.str.sprintf(core._t("'%s' is not a correct float"), value));
|
||||
}
|
||||
@@ -494,9 +517,17 @@ function parseFloatTime(value) {
|
||||
return factor * (hours + (minutes / 60));
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a String containing integer with language formating
|
||||
*
|
||||
* @param {string} value
|
||||
* The string to be parsed with the setting of thousands and
|
||||
* decimal separator
|
||||
* @returns {integer}
|
||||
* @throws {Error} if no integer is found respecting the language configuration
|
||||
*/
|
||||
function parseInteger(value) {
|
||||
value = value.replace(new RegExp(core._t.database.parameters.thousands_sep, "g"), '');
|
||||
var parsed = Number(value);
|
||||
var parsed = parseNumber(value);
|
||||
// do not accept not numbers or float values
|
||||
if (isNaN(parsed) || parsed % 1 || parsed < -2147483648 || parsed > 2147483647) {
|
||||
throw new Error(_.str.sprintf(core._t("'%s' is not a correct integer"), value));
|
||||
|
||||
@@ -128,7 +128,15 @@ QUnit.test('format one2many', function(assert) {
|
||||
});
|
||||
|
||||
QUnit.test('parse float', function(assert) {
|
||||
assert.expect(7);
|
||||
assert.expect(10);
|
||||
|
||||
var originalParameters = _.clone(core._t.database.parameters);
|
||||
|
||||
_.extend(core._t.database.parameters, {
|
||||
grouping: [3, 0],
|
||||
decimal_point: '.',
|
||||
thousands_sep: ','
|
||||
});
|
||||
|
||||
assert.strictEqual(fieldUtils.parse.float(""), 0);
|
||||
assert.strictEqual(fieldUtils.parse.float("0"), 0);
|
||||
@@ -136,16 +144,64 @@ QUnit.test('parse float', function(assert) {
|
||||
assert.strictEqual(fieldUtils.parse.float("-100.00"), -100);
|
||||
assert.strictEqual(fieldUtils.parse.float("1,000.00"), 1000);
|
||||
assert.strictEqual(fieldUtils.parse.float("1,000,000.00"), 1000000);
|
||||
assert.strictEqual(fieldUtils.parse.float('1,234.567'), 1234.567);
|
||||
assert.throws(function () {
|
||||
fieldUtils.parse.float("1.000.000");
|
||||
}, "Throw an exception if it's not a valid number");
|
||||
|
||||
var originalParameters = $.extend(true, {}, core._t.database.parameters);
|
||||
_.extend(core._t.database.parameters, {
|
||||
grouping: [3, 0],
|
||||
decimal_point: ',',
|
||||
thousands_sep: '.'
|
||||
});
|
||||
assert.strictEqual(fieldUtils.parse.float('1.234,567'), 1234.567);
|
||||
|
||||
core._t.database.parameters = originalParameters;
|
||||
assert.strictEqual(fieldUtils.parse.float('1.234,567'), 1234.567);
|
||||
assert.throws(function () {
|
||||
fieldUtils.parse.float("1,000,000");
|
||||
}, "Throw an exception if it's not a valid number");
|
||||
|
||||
_.extend(core._t.database.parameters, originalParameters);
|
||||
});
|
||||
|
||||
QUnit.test('parse integer', function(assert) {
|
||||
assert.expect(11);
|
||||
|
||||
var originalParameters = _.clone(core._t.database.parameters);
|
||||
|
||||
_.extend(core._t.database.parameters, {
|
||||
grouping: [3, 0],
|
||||
decimal_point: '.',
|
||||
thousands_sep: ','
|
||||
});
|
||||
|
||||
assert.strictEqual(fieldUtils.parse.integer(""), 0);
|
||||
assert.strictEqual(fieldUtils.parse.integer("0"), 0);
|
||||
assert.strictEqual(fieldUtils.parse.integer("100"), 100);
|
||||
assert.strictEqual(fieldUtils.parse.integer("-100"), -100);
|
||||
assert.strictEqual(fieldUtils.parse.integer("1,000"), 1000);
|
||||
assert.strictEqual(fieldUtils.parse.integer("1,000,000"), 1000000);
|
||||
assert.throws(function () {
|
||||
fieldUtils.parse.integer("1.000.000");
|
||||
}, "Throw an exception if it's not a valid number");
|
||||
assert.throws(function () {
|
||||
fieldUtils.parse.integer("1,234.567");
|
||||
}, "Throw an exception if the number is a float");
|
||||
|
||||
_.extend(core._t.database.parameters, {
|
||||
grouping: [3, 0],
|
||||
decimal_point: ',',
|
||||
thousands_sep: '.'
|
||||
});
|
||||
|
||||
assert.strictEqual(fieldUtils.parse.integer("1.000.000"), 1000000);
|
||||
assert.throws(function () {
|
||||
fieldUtils.parse.integer("1,000,000");
|
||||
}, "Throw an exception if it's not a valid number");
|
||||
assert.throws(function () {
|
||||
fieldUtils.parse.integer("1.234,567");
|
||||
}, "Throw an exception if the number is a float");
|
||||
|
||||
_.extend(core._t.database.parameters, originalParameters);
|
||||
});
|
||||
|
||||
QUnit.test('parse monetary', function(assert) {
|
||||
|
||||
Reference in New Issue
Block a user