diff options
| author | Aaron Bohy <aab@odoo.com> | 2017-05-04 10:35:55 +0200 |
|---|---|---|
| committer | Aaron Bohy <aab@odoo.com> | 2017-05-04 12:39:07 +0200 |
| commit | 7d26d244a438a71d633922e043b18e488dbe269b (patch) | |
| tree | 6fe18cecb03fb2ff7f4239e3dcb813f8de82b8e3 /addons/web/static/src/js/views/basic | |
| parent | 250e716c3a12e88acb4a199b427dc47d4ec55d5d (diff) | |
[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.
Diffstat (limited to 'addons/web/static/src/js/views/basic')
| -rw-r--r-- | addons/web/static/src/js/views/basic/basic_model.js | 27 |
1 files changed, 24 insertions, 3 deletions
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 @@ -2009,6 +2009,25 @@ var BasicModel = AbstractModel.extend({ 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; } }); |
