From 7d26d244a438a71d633922e043b18e488dbe269b Mon Sep 17 00:00:00 2001 From: Aaron Bohy Date: Thu, 4 May 2017 10:35:55 +0200 Subject: [FIX] web: BasicModel: trigger onchange when x2m is valid When a record is added in an x2many field with an onchange, the onchange is only triggered once the new record is valid (i.e. when all its required fields have a correct value). However, there was an issue in the code checking the validity of a record, as it checked, for each required field, that its value wasn't falsy (!value). This is incorrect for several type of fields: - numerical: a value of 0 is correct - boolean: should be always considered as correct - x2m: their value is a dataPoint, so it is never falsy, what should be check is the number of records inside the dataPoint. This was an issue for example in the sale.order form view, as the onchange wasn't triggered when an order line was added (with a set product), so the total amount wasn't updated correctly. --- .../web/static/src/js/views/basic/basic_model.js | 27 +++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) (limited to 'addons/web/static/src/js/views/basic/basic_model.js') diff --git a/addons/web/static/src/js/views/basic/basic_model.js b/addons/web/static/src/js/views/basic/basic_model.js index f7026f27129..42731bcf83b 100644 --- a/addons/web/static/src/js/views/basic/basic_model.js +++ b/addons/web/static/src/js/views/basic/basic_model.js @@ -2008,6 +2008,25 @@ var BasicModel = AbstractModel.extend({ var fieldsInfo = element.fieldsInfo; return Object.keys(fieldsInfo && fieldsInfo[element.viewType] || {}); }, + /** + * Returns true iff value is considered to be set for the given field's type. + * + * @private + * @param {any} value a value for the field + * @param {string} fieldType a type of field + * @returns {boolean} + */ + _isFieldSet: function (value, fieldType) { + switch (fieldType) { + case 'boolean': + return true; + case 'one2many': + case 'many2many': + return value.count > 0; + default: + return value !== false; + } + }, /** * return true if a list element is 'valid'. Such an element is valid if it * has no sub record with an unset required field. @@ -2020,11 +2039,13 @@ var BasicModel = AbstractModel.extend({ * @returns {boolean} */ _isX2ManyValid: function (id) { + var self = this; var isValid = true; var element = this.get(id, {raw: true}); - _.each(element.data, function (rec) { - _.each(rec.getFieldNames(), function (fieldName) { - if (rec.fields[fieldName].required && !rec.data[fieldName]) { + _.each(element.getFieldNames(), function (fieldName) { + var field = element.fields[fieldName]; + _.each(element.data, function (rec) { + if (field.required && !self._isFieldSet(rec.data[fieldName], field.type)) { isValid = false; } }); -- cgit v1.2.3