[FIX] web: parse date(time) values after onchange

Date and Datetime values are stored as moment instances, not
strings. This wasn't the case after an onchange, and it thus
produced a crash as we tried to call the format method on a string.
This commit is contained in:
Aaron Bohy
2017-04-11 19:44:38 +02:00
parent 2bd4497b49
commit f79399bf77
2 changed files with 55 additions and 4 deletions
@@ -539,10 +539,10 @@ var BasicModel = AbstractModel.extend({
defs.push(self._fetchNameGets(x2manyList, name));
});
} else if (field.type === 'date') {
// process date: convert into Date object
// process date: convert into a moment instance
record._changes[name] = fieldUtils.parse.date(result[name]);
} else if (field.type === 'datetime') {
// process datetime: convert into Date object
// process datetime: convert into a moment instance
record._changes[name] = fieldUtils.parse.datetime(result[name]);
} else {
record._changes[name] = result[name];
@@ -1042,6 +1042,12 @@ var BasicModel = AbstractModel.extend({
list._changes = [];
}
});
} else if (field.type === 'date') {
// process data: convert into a moment instance
record._changes[name] = fieldUtils.parse.date(val);
} else if (field.type === 'datetime') {
// process datetime: convert into a moment instance
record._changes[name] = fieldUtils.parse.datetime(val);
} else {
record._changes[name] = val;
}
@@ -1265,10 +1271,10 @@ var BasicModel = AbstractModel.extend({
record.data[name] = false;
}
} else if (field.type === 'date') {
// process data: convert into Date object
// process data: convert into a moment instance
record.data[name] = fieldUtils.parse.date(val);
} else if (field.type === 'datetime') {
// process datetime: convert into Date object
// process datetime: convert into a moment instance
record.data[name] = fieldUtils.parse.datetime(val);
}
});
@@ -1817,6 +1817,51 @@ QUnit.module('Views', {
form.destroy();
});
QUnit.test('onchanges on date(time) fields', function (assert) {
assert.expect(6);
this.data.partner.onchanges = {
foo: function (obj) {
obj.date = '2021-12-12';
obj.datetime = '2021-12-12 10:55:05';
},
};
var form = createView({
View: FormView,
model: 'partner',
data: this.data,
arch: '<form string="Partners">' +
'<group>' +
'<field name="foo"/>' +
'<field name="date"/>' +
'<field name="datetime"/>' +
'</group>' +
'</form>',
res_id: 1,
});
assert.strictEqual(form.$('.o_form_field[name=date]').text(),
'01/25/2017', "the initial date should be correct");
assert.strictEqual(form.$('.o_form_field[name=datetime]').text(),
'12/12/2016 10:55:05', "the initial datetime should be correct");
form.$buttons.find('.o_form_button_edit').click();
assert.strictEqual(form.$('.o_form_field[name=date] input').val(),
'01/25/2017', "the initial date should be correct in edit");
assert.strictEqual(form.$('.o_form_field[name=datetime] input').val(),
'12/12/2016 10:55:05', "the initial datetime should be correct in edit");
// trigger the onchange
form.$('.o_form_field[name="foo"]').val("coucou").trigger('input');
assert.strictEqual(form.$('.o_form_field[name=date] input').val(),
'12/12/2021', "the initial date should be correct in edit");
assert.strictEqual(form.$('.o_form_field[name=datetime] input').val(),
'12/12/2021 10:55:05', "the initial datetime should be correct in edit");
form.destroy();
});
});
});