[FIX] web: datepicker: crash on invalid date
There is an open issue on the tempusdominus lib: https://github.com/tempusdominus/bootstrap-4/issues/223 It occurs when there is a valid value in a date(time) field, and the user unsets it by setting an invalid one. This rev. fixes the issue in Odoo by preventing to reach the buggy piece of code in the lib. In a few words, when the user sets an invalid date, we reset the previous valid date (which is what the lib tries to do anyway). Task 2057009 closes odoo/odoo#36211 Signed-off-by: Aaron Bohy (aab) <aab@odoo.com> Co-authored-by: Aaron Bohy <aab@odoo.com>
This commit is contained in:
committed by
Aaron Bohy
co-authored by
Aaron Bohy
parent
85c5d0a4be
commit
fbbfa6ddca
@@ -84,8 +84,8 @@ var DateWidget = Widget.extend({
|
|||||||
* set datetime value
|
* set datetime value
|
||||||
*/
|
*/
|
||||||
changeDatetime: function () {
|
changeDatetime: function () {
|
||||||
|
var oldValue = this.getValue();
|
||||||
if (this.isValid()) {
|
if (this.isValid()) {
|
||||||
var oldValue = this.getValue();
|
|
||||||
this._setValueFromUi();
|
this._setValueFromUi();
|
||||||
var newValue = this.getValue();
|
var newValue = this.getValue();
|
||||||
var hasChanged = !oldValue !== !newValue;
|
var hasChanged = !oldValue !== !newValue;
|
||||||
@@ -102,6 +102,9 @@ var DateWidget = Widget.extend({
|
|||||||
}
|
}
|
||||||
this.trigger("datetime_changed");
|
this.trigger("datetime_changed");
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
var formattedValue = oldValue ? this._formatClient(oldValue) : null;
|
||||||
|
this.$input.val(formattedValue);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -2696,8 +2696,32 @@ QUnit.module('basic_fields', {
|
|||||||
form.destroy();
|
form.destroy();
|
||||||
});
|
});
|
||||||
|
|
||||||
QUnit.test('date field should remove the date if the date is not valid', function (assert) {
|
QUnit.test('date field: set an invalid date when the field is already set', function (assert) {
|
||||||
assert.expect(1);
|
assert.expect(2);
|
||||||
|
|
||||||
|
var form = createView({
|
||||||
|
View: FormView,
|
||||||
|
model: 'partner',
|
||||||
|
data: this.data,
|
||||||
|
arch: '<form string="Partners"><field name="date"/></form>',
|
||||||
|
res_id: 1,
|
||||||
|
viewOptions: {
|
||||||
|
mode: 'edit',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
var $input = form.$('.o_field_widget[name=date] input');
|
||||||
|
|
||||||
|
assert.strictEqual($input.val(), "02/03/2017");
|
||||||
|
|
||||||
|
$input.val('mmmh').trigger('change');
|
||||||
|
assert.strictEqual($input.val(), "02/03/2017", "should have reset the original value");
|
||||||
|
|
||||||
|
form.destroy();
|
||||||
|
});
|
||||||
|
|
||||||
|
QUnit.test('date field: set an invalid date when the field is not set yet', function (assert) {
|
||||||
|
assert.expect(2);
|
||||||
|
|
||||||
var form = createView({
|
var form = createView({
|
||||||
View: FormView,
|
View: FormView,
|
||||||
@@ -2705,13 +2729,18 @@ QUnit.module('basic_fields', {
|
|||||||
data: this.data,
|
data: this.data,
|
||||||
arch: '<form string="Partners"><field name="date"/></form>',
|
arch: '<form string="Partners"><field name="date"/></form>',
|
||||||
res_id: 4,
|
res_id: 4,
|
||||||
|
viewOptions: {
|
||||||
|
mode: 'edit',
|
||||||
|
},
|
||||||
});
|
});
|
||||||
// switch to edit mode
|
|
||||||
form.$buttons.find('.o_form_button_edit').click();
|
|
||||||
// set an invalid date
|
|
||||||
var $input = form.$('.o_field_widget[name=date] input');
|
var $input = form.$('.o_field_widget[name=date] input');
|
||||||
|
|
||||||
|
assert.strictEqual($input.text(), "");
|
||||||
|
|
||||||
$input.val('mmmh').trigger('change');
|
$input.val('mmmh').trigger('change');
|
||||||
assert.strictEqual($input.text(), "", "The date field should be empty");
|
assert.strictEqual($input.text(), "", "The date field should be empty");
|
||||||
|
|
||||||
form.destroy();
|
form.destroy();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user