[FIX] web: do not create new line if onchange has warning in o2m

In a one2many field, when the user clicks on the 'Add a line' button, we
need to create a new record.  This means that we call the default_get
route, then call the onchanges.  The onchange can return a 'warning'
key, which is displayed to the user.  But in the specific case of a
one2many, we don't want to actually create the record.  So, we reject the
deferred to prevent that from happening.
This commit is contained in:
Géry Debongnie
2017-04-11 19:44:38 +02:00
committed by Aaron Bohy
parent 28e1620731
commit 21324ded09
2 changed files with 51 additions and 1 deletions
@@ -770,6 +770,7 @@ var BasicModel = AbstractModel.extend({
var onchangeDef;
if (onChangeFields.length) {
onchangeDef = self._applyOnChange(record, onChangeFields).then(function (result) {
delete record._warning;
return _.keys(changes).concat(Object.keys(result && result.value || {}));
});
} else {
@@ -884,6 +885,7 @@ var BasicModel = AbstractModel.extend({
title: result.warning.title,
type: 'dialog',
});
record._warning = true;
}
var defs = [];
_.each(result.value, function (val, name) {
@@ -2045,7 +2047,11 @@ var BasicModel = AbstractModel.extend({
}
}
if (shouldApplyOnchange) {
return self._applyOnChange(record, fields_key);
return self._applyOnChange(record, fields_key).then(function () {
if (record._warning) {
return $.Deferred().reject();
}
});
} else {
return $.when();
}
@@ -1868,6 +1868,50 @@ QUnit.module('Views', {
form.destroy();
});
QUnit.test('do nothing if add a line in one2many result in a onchange with a warning', function (assert) {
assert.expect(2);
this.data.partner.onchanges = { foo: true };
var form = createView({
View: FormView,
model: 'partner',
data: this.data,
arch: '<form string="Partners">' +
'<field name="p">' +
'<tree editable="top">' +
'<field name="foo"/>' +
'</tree>' +
'</field>' +
'</form>',
res_id: 2,
mockRPC: function (route, args) {
if (args.method === 'onchange') {
return $.when({
value: {},
warning: {
title: "Warning",
message: "You must first select a partner"
}
});
}
return this._super.apply(this, arguments);
},
intercepts: {
warning: function () {
assert.step("should have triggered a warning");
},
},
});
// go to edit mode, click to add a record in the o2m
form.$buttons.find('.o_form_button_edit').click();
form.$('.o_form_field_x2many_list_row_add a').click();
assert.strictEqual(form.$('tr.o_data_row').length, 0,
"should not have added a line");
form.destroy();
});
QUnit.test('attrs are properly transmitted to new records', function (assert) {
assert.expect(2);