[FIX] web: selection: required, option present but disabled

Before this commit, a field selection which was required
did not have an empty option
ref: 4dfabb8f7a

This is fine, but overlooked the fact that modules can modify
the required attribute, or even views can do it
So, we may end up in a situation where the field is required but set to false
This configuration is problematic, at least for the timezone selection field
(see OPW)

After this commit, the empty option is just not visible
note that `disabled = True` would not work because val is not accessible

Because of commits:
- 4dfabb8f7a for the selection feature
- d77ce4c2a9 overriding the required attribute of tz in res.user form

OPW 2057913

closes odoo/odoo#35989

Signed-off-by: Lucas Perais (lpe) <lpe@odoo.com>
This commit is contained in:
Lucas Perais (lpe)
2019-08-23 14:29:05 +00:00
parent f01c325e9a
commit d310cd4478
2 changed files with 37 additions and 12 deletions
@@ -2471,10 +2471,14 @@ var FieldSelection = AbstractField.extend({
*/
_renderEdit: function () {
this.$el.empty();
var required = this.attrs.modifiersValue && this.attrs.modifiersValue.required;
for (var i = 0 ; i < this.values.length ; i++) {
var disabled = required && this.values[i][0] === false;
this.$el.append($('<option/>', {
value: JSON.stringify(this.values[i][0]),
text: this.values[i][1]
text: this.values[i][1],
style: disabled ? "display: none" : "",
}));
}
var value = this.value;
@@ -2513,9 +2517,7 @@ var FieldSelection = AbstractField.extend({
return v[0] === false && v[1] === '';
});
}
if (!this.attrs.modifiersValue || !this.attrs.modifiersValue.required) {
this.values = [[false, this.attrs.placeholder || '']].concat(this.values);
}
this.values = [[false, this.attrs.placeholder || '']].concat(this.values);
},
//--------------------------------------------------------------------------
@@ -1189,7 +1189,7 @@ QUnit.module('relational_fields', {
});
QUnit.test('required selection widget should not have blank option', async function (assert) {
assert.expect(3);
assert.expect(12);
this.data.partner.fields.feedback_value = {
type: "selection",
@@ -1210,15 +1210,38 @@ QUnit.module('relational_fields', {
});
await testUtils.form.clickEdit(form);
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");
var $colorField = form.$('.o_field_widget[name=color]');
assert.containsN($colorField, 'option', 3, "Three options in non required field");
assert.hasAttrValue($colorField.find('option:first()'), 'style', "",
"Should not have display=none");
assert.hasAttrValue($colorField.find('option:eq(1)'), 'style', "",
"Should not have display=none");
assert.hasAttrValue($colorField.find('option:eq(2)'), 'style', "",
"Should not have display=none");
const $requiredSelect = form.$('.o_field_widget[name=feedback_value]');
assert.containsN($requiredSelect, 'option', 3, "Three options in required field");
assert.hasAttrValue($requiredSelect.find('option:first()'), 'style', "display: none",
"Should have display=none");
assert.hasAttrValue($requiredSelect.find('option:eq(1)'), 'style', "",
"Should not have display=none");
assert.hasAttrValue($requiredSelect.find('option:eq(2)'), 'style', "",
"Should not have display=none");
// change value to update widget modifier values
await testUtils.fields.editSelect(form.$('.o_field_widget[name=feedback_value]'), '"bad"');
assert.strictEqual(form.$('.o_field_widget[name=color]')[0].options.length, 2,
"should have only 2 options");
await testUtils.fields.editSelect($requiredSelect, '"bad"');
$colorField = form.$('.o_field_widget[name=color]');
assert.containsN($colorField, 'option', 3, "Three options in required field");
assert.hasAttrValue($colorField.find('option:first()'), 'style', "display: none",
"Should have display=none");
assert.hasAttrValue($colorField.find('option:eq(1)'), 'style', "",
"Should not have display=none");
assert.hasAttrValue($colorField.find('option:eq(2)'), 'style', "",
"Should not have display=none");
form.destroy();
});