[FIX] web: date_picker empty input field

When you try to empty a date or datetime field, you get a traceback
saying that 'clone' and 'isSame' are not functions.

This error occurs when you empty the field because the value is 'false'
and you cannot call 'clone' or 'isSame' on false.

To fix this, we ensure that value is not false before calling these
methods on it.

This bug has been introduced in rev: https://github.com/odoo/odoo/commit/c32724eae06c987e74a0600f1669696e499edc33
This commit is contained in:
Pierre Masereel
2017-04-18 13:14:50 +02:00
parent a055d5f226
commit 2b22ceea6f
3 changed files with 78 additions and 3 deletions
@@ -268,7 +268,7 @@ var FieldDate = InputField.extend({
this.datewidget = this._makeDatePicker();
this.datewidget.on('datetime_changed', this, function () {
var value = this._getDateWidgetValue();
if (!value.isSame(this.value)) {
if ((!value && this.value) || (value && !value.isSame(this.value))) {
this._setValue(value);
}
});
@@ -330,7 +330,8 @@ var FieldDateTime = FieldDate.extend({
* @private
*/
_getDateWidgetValue: function () {
return this.datewidget.get_value().add(-this.getSession().tzOffset, 'minutes');
var value = this.datewidget.get_value();
return value && value.add(-this.getSession().tzOffset, 'minutes');
},
/**
@@ -60,7 +60,7 @@ var DateWidget = Widget.extend({
}
},
get_value: function() {
return this.get('value').clone();
return this.get('value') && this.get('value').clone();
},
set_value_from_ui: function() {
var value = this.$input.val() || false;
@@ -1082,6 +1082,41 @@ QUnit.module('basic_fields', {
list.destroy();
});
QUnit.test('date field remove value', function (assert) {
assert.expect(4);
var form = createView({
View: FormView,
model: 'partner',
data: this.data,
arch:'<form string="Partners"><field name="date"/></form>',
res_id: 1,
mockRPC: function (route, args) {
if (route === '/web/dataset/call_kw/partner/write') {
assert.strictEqual(args.args[1].date, false, 'the correct value should be saved');
}
return this._super.apply(this, arguments);
},
translateParameters: { // Avoid issues due to localization formats
date_format: '%m/%d/%Y',
},
});
// switch to edit mode
form.$buttons.find('.o_form_button_edit').click();
assert.strictEqual(form.$('.o_datepicker_input').val(), '02/03/2017',
'the date should be correct in edit mode');
$('.o_datepicker_input').val('').trigger('input').trigger('change');
assert.ok(!form.$('.o_datepicker_input').val());
// save
form.$buttons.find('.o_form_button_save').click();
assert.strictEqual(form.$('.o_form_field_date').text(), '',
'the selected date should be displayed after saving');
form.destroy();
});
QUnit.module('FieldDatetime');
@@ -1206,6 +1241,45 @@ QUnit.module('basic_fields', {
list.destroy();
});
QUnit.test('datetime field remove value', function (assert) {
assert.expect(4);
var form = createView({
View: FormView,
model: 'partner',
data: this.data,
arch:'<form string="Partners"><field name="datetime"/></form>',
res_id: 1,
mockRPC: function (route, args) {
if (route === '/web/dataset/call_kw/partner/write') {
assert.strictEqual(args.args[1].datetime, false, 'the correct value should be saved');
}
return this._super.apply(this, arguments);
},
translateParameters: { // Avoid issues due to localization formats
date_format: '%m/%d/%Y',
time_format: '%H:%M:%S',
},
session: {
tzOffset: 120
},
});
// switch to edit mode
form.$buttons.find('.o_form_button_edit').click();
assert.strictEqual(form.$('.o_datepicker_input').val(), '02/08/2017 12:00:00',
'the date time should be correct in edit mode');
$('.o_datepicker_input').val('').trigger('input').trigger('change');
assert.ok(!form.$('.o_datepicker_input').val());
// save
form.$buttons.find('.o_form_button_save').click();
assert.strictEqual(form.$('.o_form_field_date').text(), '',
'the selected date should be displayed after saving');
form.destroy();
});
QUnit.module('FieldMonetary');