From fc58719d0740cd8d4029712b47b19f030d46f717 Mon Sep 17 00:00:00 2001 From: MerlinGuillaume Date: Thu, 7 Jul 2022 10:20:17 +0000 Subject: [PATCH] [FIX] web: edit radio and boolean widget fields Fields with widget `radio` and `boolean` can't be edited Steps to reproduce: 1. Install Discuss 2. Connect as demo and go to your preferences (top right) 3. You can't edit your notification type Solution: Use widgetOptions' mode along with hasReadonlyModifier to disable the fields Problem: This PR https://github.com/odoo/odoo/pull/88223 modified the value of hasReadonlyModifier which is used to disable radio and boolean fields opw-2898464 closes odoo/odoo#97106 X-original-commit: 6befccd7c8ac22fe4da2a741efeecde2d036a7b9 Signed-off-by: Guillaume Merlin (megu) --- .../src/legacy/js/_deprecated/basic_fields.js | 2 +- .../src/legacy/js/fields/relational_fields.js | 2 +- .../tests/legacy/fields/basic_fields_tests.js | 62 +++++++++++++++++++ .../legacy/fields/relational_fields_tests.js | 62 +++++++++++++++++++ 4 files changed, 126 insertions(+), 2 deletions(-) diff --git a/addons/web/static/src/legacy/js/_deprecated/basic_fields.js b/addons/web/static/src/legacy/js/_deprecated/basic_fields.js index eae71a11295..34a3c2e3b39 100644 --- a/addons/web/static/src/legacy/js/_deprecated/basic_fields.js +++ b/addons/web/static/src/legacy/js/_deprecated/basic_fields.js @@ -126,7 +126,7 @@ var FieldBoolean = AbstractField.extend({ _render: function () { var $checkbox = this._formatValue(this.value); this.$input = $checkbox.find('input'); - this.$input.prop('disabled', this.hasReadonlyModifier); + this.$input.prop('disabled', this.hasReadonlyModifier && this.mode != 'edit'); this.$el.addClass($checkbox.attr('class')); this.$el.empty().append($checkbox.contents()); }, diff --git a/addons/web/static/src/legacy/js/fields/relational_fields.js b/addons/web/static/src/legacy/js/fields/relational_fields.js index 4649aa2963e..0328f650938 100644 --- a/addons/web/static/src/legacy/js/fields/relational_fields.js +++ b/addons/web/static/src/legacy/js/fields/relational_fields.js @@ -3544,7 +3544,7 @@ var FieldRadio = FieldSelection.extend({ index: index, name: self.unique_id, value: value, - disabled: self.hasReadonlyModifier, + disabled: self.hasReadonlyModifier && self.mode != 'edit', })); }); }, diff --git a/addons/web/static/tests/legacy/fields/basic_fields_tests.js b/addons/web/static/tests/legacy/fields/basic_fields_tests.js index 4e8e700f6d5..1b9cf556467 100644 --- a/addons/web/static/tests/legacy/fields/basic_fields_tests.js +++ b/addons/web/static/tests/legacy/fields/basic_fields_tests.js @@ -405,6 +405,68 @@ QUnit.module('Legacy basic_fields', { form.destroy(); }); + QUnit.test('boolean field is editable in an editable form', async function (assert) { + assert.expect(2); + + const form = await createView({ + View: FormView, + model: 'partner', + data: this.data, + arch: '
' + + '' + + '', + }); + + assert.containsOnce(form, '.o_field_boolean input:enabled', + "the field should be editable"); + + await testUtils.form.clickSave(form); + + assert.containsOnce(form, '.o_field_boolean input:enabled', + "the field should be editable"); + + form.destroy(); + }); + + QUnit.test('boolean field is not editable in a readonly form', async function (assert) { + assert.expect(1); + + const form = await createView({ + View: FormView, + model: 'partner', + data: this.data, + arch: '
' + + '' + + '', + viewOptions: { + mode: 'readonly', + }, + }); + + assert.containsOnce(form, '.o_field_boolean input:disabled', + "the field should not be editable"); + + form.destroy(); + }); + + QUnit.test('boolean field is not editable with a readonly modifier', async function (assert) { + assert.expect(1); + + const form = await createView({ + View: FormView, + model: 'partner', + data: this.data, + arch: '
' + + '' + + '', + }); + + assert.containsOnce(form, '.o_field_boolean input:disabled', + "the field should not be editable"); + + form.destroy(); + }); + QUnit.module('FieldBooleanToggle'); QUnit.test('use boolean toggle widget in form view', async function (assert) { diff --git a/addons/web/static/tests/legacy/fields/relational_fields_tests.js b/addons/web/static/tests/legacy/fields/relational_fields_tests.js index 82a2f7ce5b4..fbba3823add 100644 --- a/addons/web/static/tests/legacy/fields/relational_fields_tests.js +++ b/addons/web/static/tests/legacy/fields/relational_fields_tests.js @@ -2357,6 +2357,68 @@ QUnit.module('Legacy relational_fields', { form.destroy(); }); + QUnit.test('radio field is editable in an editable form', async function (assert) { + assert.expect(2); + + const form = await createView({ + View: FormView, + model: 'partner', + data: this.data, + arch: '
' + + '' + + '', + }); + + assert.containsN(form, '.o_field_radio input:enabled', 2, + "the field should be editable"); + + await testUtils.form.clickSave(form); + + assert.containsN(form, '.o_field_radio input:enabled', 2, + "the field should be editable"); + + form.destroy(); + }); + + QUnit.test('radio field is not editable in a readonly form', async function (assert) { + assert.expect(1); + + const form = await createView({ + View: FormView, + model: 'partner', + data: this.data, + arch: '
' + + '' + + '', + viewOptions: { + mode: 'readonly', + }, + }); + + assert.containsN(form, '.o_field_radio input:disabled', 2, + "the field should not be editable"); + + form.destroy(); + }); + + QUnit.test('radio field is not editable with a readonly modifier', async function (assert) { + assert.expect(1); + + const form = await createView({ + View: FormView, + model: 'partner', + data: this.data, + arch: '
' + + '' + + '', + }); + + assert.containsN(form, '.o_field_radio input:disabled', 2, + "the field should not be editable"); + + form.destroy(); + }); + QUnit.test('fieldradio change value by onchange', async function (assert) { assert.expect(4);