diff --git a/addons/web/static/src/js/fields/relational_fields.js b/addons/web/static/src/js/fields/relational_fields.js index 936a27c739b..b8eb106caab 100644 --- a/addons/web/static/src/js/fields/relational_fields.js +++ b/addons/web/static/src/js/fields/relational_fields.js @@ -2166,6 +2166,19 @@ var FieldSelection = AbstractField.extend({ isSet: function () { return this.value !== false; }, + /** + * Listen to modifiers updates to hide/show the falsy value in the dropdown + * according to the required modifier. + * + * @override + */ + updateModifiersValue: function () { + this._super.apply(this, arguments); + if (!this.attrs.modifiersValue.invisible && this.mode !== 'readonly') { + this._setValues(); + this._renderEdit(); + } + }, //-------------------------------------------------------------------------- // Private @@ -2219,7 +2232,9 @@ var FieldSelection = AbstractField.extend({ return v[0] === false && v[1] === ''; }); } - this.values = [[false, this.attrs.placeholder || '']].concat(this.values); + if (!this.attrs.modifiersValue || !this.attrs.modifiersValue.required) { + this.values = [[false, this.attrs.placeholder || '']].concat(this.values); + } }, //-------------------------------------------------------------------------- diff --git a/addons/web/static/tests/fields/relational_fields_tests.js b/addons/web/static/tests/fields/relational_fields_tests.js index 62821027c5b..950a3dcd8fe 100644 --- a/addons/web/static/tests/fields/relational_fields_tests.js +++ b/addons/web/static/tests/fields/relational_fields_tests.js @@ -11558,6 +11558,41 @@ QUnit.module('relational_fields', { form.destroy(); }); + QUnit.test('required selection widget should not have blank option', function (assert) { + assert.expect(3); + + this.data.partner.fields.feedback_value = { + type: "selection", + required: true, + selection : [['good', 'Good'], ['bad', 'Bad']], + default: 'good', + string: 'Good' + }; + var form = createView({ + View: FormView, + model: 'partner', + data: this.data, + arch: '
', + res_id: 1 + }); + + form.$buttons.find('.o_form_button_edit').click(); + assert.strictEqual(form.$('.o_field_widget[name=color]')[0].options.length, 3, + "non required selection field must have 3 options 1 blank option and 2 value options"); + assert.strictEqual(form.$('.o_field_widget[name=feedback_value]')[0].options.length, 2, + "should have only 2 options without blank option"); + + // change value to update widget modifier values + form.$('.o_field_widget[name=feedback_value]').val('"bad"').trigger('change'); + assert.strictEqual(form.$('.o_field_widget[name=color]')[0].options.length, 2, + "should have only 2 options"); + + form.destroy(); + }); + QUnit.module('FieldMany2ManyTags'); QUnit.test('fieldmany2many tags with and without color', function (assert) {