[IMP] web: FieldSelection: no empty value if required

From this rev., the empty value isn't available in the options of
required selection fields, thus preventing the user to unset it.

Related to task #49262
Closes #24404
This commit is contained in:
Dharmang Soni
2018-08-14 13:51:07 +02:00
committed by Aaron Bohy
parent 92bdd0e7c5
commit 4dfabb8f7a
2 changed files with 51 additions and 1 deletions
@@ -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);
}
},
//--------------------------------------------------------------------------
@@ -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: '<form string="Partners">' +
'<field name="feedback_value"/>' +
'<field name="color" attrs="{\'required\': [(\'feedback_value\', \'=\', \'bad\')]}"/>' +
'</form>',
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) {