From 6fa63eb15ca809fce2770f8ee75af592a4609c6a Mon Sep 17 00:00:00 2001 From: Aaron Bohy Date: Wed, 25 Oct 2017 13:18:35 +0200 Subject: [PATCH] [FIX] web: default value for o2m inside o2m A traceback was raised when opening a new record for a model with a one2many displayed inside a one2many (e.g. simply displaying the number of records in the relation), and with an onchange setting a default value to the inner one2many (for example, linking it to existing records). As the inner o2m has no subviews, it has no fieldsInfo, and the code assumed that fieldsInfo was always set. This was for example reproducible from v11 as follows: - create a product (with MTO/Buy and call for tender (need purchase agreement module)) - create an SO with this product and confirm it - go to purchase order form view and add a move_dest_ids field in the tree definition of field order_line - go to menu purchase agreement and choose the PA generated by your SO - add a vendor, confirm the PA and click on 'New Quotation'. --- .../static/src/js/views/basic/basic_model.js | 3 +- .../tests/fields/relational_fields_tests.js | 41 ++++++++++++++++++- 2 files changed, 42 insertions(+), 2 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 1d8e68c3e62..2cf96815b5e 100644 --- a/addons/web/static/src/js/views/basic/basic_model.js +++ b/addons/web/static/src/js/views/basic/basic_model.js @@ -2881,7 +2881,8 @@ var BasicModel = AbstractModel.extend({ * @returns {boolean} */ _isFieldProtected: function (record, fieldName, viewType) { - var fieldInfo = record.fieldsInfo[viewType || record.viewType][fieldName]; + var fieldInfo = record.fieldsInfo && + (record.fieldsInfo[viewType || record.viewType][fieldName]); if (fieldInfo) { var rawModifiers = JSON.parse(fieldInfo.modifiers || "{}"); var modifiers = this._evalModifiers(record, rawModifiers); diff --git a/addons/web/static/tests/fields/relational_fields_tests.js b/addons/web/static/tests/fields/relational_fields_tests.js index 91102d8dd90..2f7253abba0 100644 --- a/addons/web/static/tests/fields/relational_fields_tests.js +++ b/addons/web/static/tests/fields/relational_fields_tests.js @@ -5990,6 +5990,46 @@ QUnit.module('relational_fields', { form.destroy(); }); + QUnit.test('default value for nested one2manys (coming from onchange)', function (assert) { + assert.expect(3); + + this.data.partner.onchanges.p = function (obj) { + obj.p = [ + [5], + [0, 0, {turtles: [[5], [4, 1]]}], // link record 1 by default + ]; + }; + + var form = createView({ + View: FormView, + model: 'partner', + data: this.data, + arch: '
' + + '' + + '' + + '' + + '' + + '' + + '
', + mockRPC: function (route, args) { + if (args.method === 'create') { + assert.strictEqual(args.args[0].p[0][0], 0, + "should send a command 0 (CREATE) for p"); + assert.deepEqual(args.args[0].p[0][2], {turtles: [[4, 1, false]]}, + "should send the correct values"); + } + return this._super.apply(this, arguments); + }, + }); + + assert.strictEqual(form.$('.o_data_cell').text(), '1 record', + "should correctly display the value of the inner o2m"); + + form.$buttons.find('.o_form_button_save').click(); + + form.destroy(); + }); + QUnit.test('display correct value after validation error', function (assert) { assert.expect(4); @@ -6079,7 +6119,6 @@ QUnit.module('relational_fields', { form.destroy(); }); - QUnit.module('FieldMany2Many'); QUnit.test('many2many kanban: edition', function (assert) {