[FIX] web: set 0 as default value for numeric fields

When creating a new record, the numeric field default value should be 0 in order
to avoid manually setting mandatory fields to 0.

This behaviour has been removed with the new views but one wants to restore it.

The `mockRead` function has also been adapted in this commit because the server
returns 0 for unset numeric fields.
This commit is contained in:
Martin Geubelle
2017-06-22 10:52:26 +02:00
parent 3de0715f12
commit c62a4edcb0
6 changed files with 40 additions and 31 deletions
@@ -2198,12 +2198,6 @@ var BasicModel = AbstractModel.extend({
if (!field || field.name === 'id') {
continue;
}
if (field.type === 'float' ||
field.type === 'integer' ||
field.type === 'monetary') {
context[fieldName] = context[fieldName] || 0;
continue;
}
if (field.type === 'date' || field.type === 'datetime') {
if (context[fieldName]) {
context[fieldName] = JSON.parse(JSON.stringify(context[fieldName]));
@@ -2449,7 +2443,11 @@ var BasicModel = AbstractModel.extend({
var fieldName = fieldNames[i];
if (!(fieldName in result)) {
var field = params.fields[fieldName];
if (field.type === 'one2many' || field.type === 'many2many') {
if (field.type === 'float' ||
field.type === 'integer' ||
field.type === 'monetary') {
result[fieldName] = 0;
} else if (field.type === 'one2many' || field.type === 'many2many') {
result[fieldName] = [];
} else {
result[fieldName] = null;
@@ -394,7 +394,7 @@ QUnit.module('basic_fields', {
QUnit.module('FieldFloat');
QUnit.test('float field when unset', function (assert) {
assert.expect(1);
assert.expect(2);
var form = createView({
View: FormView,
@@ -408,8 +408,10 @@ QUnit.module('basic_fields', {
res_id: 4,
});
assert.ok(form.$('.o_field_widget').hasClass('o_field_empty'),
'Non-set float field should be recognized as unset.');
assert.notOk(form.$('.o_field_widget').hasClass('o_field_empty'),
'Non-set float field should be considered as 0.');
assert.strictEqual(form.$('.o_field_widget').text(), "0.000",
'Non-set float field should be considered as 0.');
form.destroy();
});
@@ -1955,7 +1957,7 @@ QUnit.module('basic_fields', {
QUnit.module('FieldInteger');
QUnit.test('integer field when unset', function (assert) {
assert.expect(1);
assert.expect(2);
var form = createView({
View: FormView,
@@ -1965,8 +1967,10 @@ QUnit.module('basic_fields', {
res_id: 4,
});
assert.ok(form.$('.o_field_widget').hasClass('o_field_empty'),
'Non-set integer field should be recognized as unset.');
assert.notOk(form.$('.o_field_widget').hasClass('o_field_empty'),
'Non-set integer field should be recognized as 0.');
assert.strictEqual(form.$('.o_field_widget').text(), "0",
'Non-set integer field should be recognized as 0.');
form.destroy();
});
@@ -2927,8 +2927,8 @@ QUnit.module('relational_fields', {
assert.strictEqual(form.$('.o_data_row').length, 3,
"sould have 3 records in one2many list");
assert.strictEqual(form.$('.o_data_row').text(), "blip1.59yop1.5tototo1.550xphone",
"sould display the record values in one2many list");
assert.strictEqual(form.$('.o_data_row').text(), "blip1.59yop1.50tototo1.550xphone",
"should display the record values in one2many list");
$('.o_form_button_save').click();
@@ -539,7 +539,7 @@ var MockServer = Class.extend({
var fields = args[1] && args[1].length ? _.uniq(args[1].concat(['id'])) : Object.keys(this.data[model].fields);
var records = _.reduce(ids, function (records, id) {
var record = _.findWhere(self.data[model].records, {id: id});
return record ? records.concat(record) : records
return record ? records.concat(record) : records;
}, []);
var results = _.map(records, function (record) {
var result = {};
@@ -549,7 +549,12 @@ var MockServer = Class.extend({
// the field doens't exist on the model, so skip it
continue;
}
if (field.type === 'many2one') {
if (field.type === 'float' ||
field.type === 'integer' ||
field.type === 'monetary') {
// read should return 0 for unset numeric fields
result[fields[i]] = record[fields[i]] || 0;
} else if (field.type === 'many2one') {
var relatedRecord = _.findWhere(self.data[field.relation].records, {
id: record[fields[i]]
});
@@ -1364,8 +1364,8 @@ QUnit.module('Views', {
delete this.params.res_id;
model.load(this.params).then(function (resultID) {
var record = model.get(resultID);
assert.strictEqual(record.data.bar, false,
"should be initialized with correct value");
assert.strictEqual(record.data.bar, 0,
"should be initialized with correct value (0 as integer)");
model.notifyChanges(resultID, {foo: "anotherverylongstring"});
+14 -12
View File
@@ -625,10 +625,10 @@ QUnit.module('Views', {
res_id: 2,
});
assert.strictEqual(form.$('.o_field_widget.o_field_empty').length, 2,
"should have 2 empty fields with correct class");
assert.strictEqual(form.$('.o_form_label_empty').length, 2,
"should have 2 muted labels (for the empty fieds) in readonly");
assert.strictEqual(form.$('.o_field_widget.o_field_empty').length, 1,
"should have 1 empty field with correct class");
assert.strictEqual(form.$('.o_form_label_empty').length, 1,
"should have 1 muted label (for the empty fied) in readonly");
form.$buttons.find('.o_form_button_edit').click();
@@ -771,7 +771,7 @@ QUnit.module('Views', {
});
QUnit.test('required float fields works as expected', function (assert) {
assert.expect(8);
assert.expect(10);
this.data.partner.fields.qux.required = true;
var form = createView({
@@ -793,24 +793,26 @@ QUnit.module('Views', {
assert.ok(form.$('input[name="qux"]').hasClass('o_required_modifier'),
"qux input is flagged as required");
assert.strictEqual(form.$('input[name="qux"]').val(), "",
"qux input is empty");
assert.strictEqual(form.$('input[name="qux"]').val(), "0.0",
"qux input is 0 by default (float field)");
form.$buttons.find('.o_form_button_save').click();
assert.ok(form.$('input[name="qux"]').hasClass('o_field_invalid'),
"qux input is displayed as invalid");
assert.notOk(form.$('input[name="qux"]').hasClass('o_field_invalid'),
"qux input is not displayed as invalid");
form.$('input[name="qux"]').val("0").trigger('input');
form.$buttons.find('.o_form_button_edit').click();
form.$('input[name="qux"]').val("1").trigger('input');
form.$buttons.find('.o_form_button_save').click();
form.$buttons.find('.o_form_button_edit').click();
assert.strictEqual(form.$('input[name="qux"]').val(), "0.0",
assert.strictEqual(form.$('input[name="qux"]').val(), "1.0",
"qux input is properly formatted");
assert.verifySteps(['default_get', 'create', 'read']);
assert.verifySteps(['default_get', 'create', 'read', 'write', 'read']);
form.destroy();
});