[FIX] web: manually enter value in daterange throws error

before this commit: if user enters some random value in daterange widget and
click outside the daterange field it throws traceback while it should not throw
traceback, it should only warn user that you are entering wrong value.

after this commit: if user enters some random value in daterange widget then it
will show warning toaster that you are entering wrong value.

task-2410523

closes odoo/odoo#73366

X-original-commit: 588d133f3daf28e0ce69052ae57ad89c5a96eb3a
Signed-off-by: Simon Genin (ges@odoo) <ges@odoo.com>
This commit is contained in:
Mohammed Shekha
2021-07-07 11:09:19 +00:00
parent d03de125b9
commit 38fc6a6e80
2 changed files with 76 additions and 1 deletions
@@ -695,6 +695,31 @@ var FieldDateRange = InputField.extend({
}
this._super.apply(this, arguments);
},
//--------------------------------------------------------------------------
// Public
//--------------------------------------------------------------------------
/**
* Field widget is valid if value entered can convered to date/dateime value
* while parsing input value to date/datetime throws error then widget considered
* invalid
*
* @override
*/
isValid: function () {
const value = this.mode === "readonly" ? this.value : this.$input.val();
try {
return field_utils.parse[this.formatType](value, this.field, { timezone: true }) || true;
} catch (error) {
return false;
}
},
//--------------------------------------------------------------------------
// Private
//--------------------------------------------------------------------------
/**
* Return the date written in the input, in UTC.
*
@@ -702,7 +727,14 @@ var FieldDateRange = InputField.extend({
* @returns {Moment|false}
*/
_getValue: function () {
return field_utils.parse[this.formatType](this.$input.val(), this.field, { timezone: true });
try {
// user may enter manual value in input and it may not be parsed as date/datetime value
this.removeInvalidClass();
return field_utils.parse[this.formatType](this.$input.val(), this.field, { timezone: true });
} catch (error) {
this.setInvalidClass();
return false;
}
},
//--------------------------------------------------------------------------
@@ -3852,6 +3852,49 @@ QUnit.module('basic_fields', {
form.destroy();
});
QUnit.test('Daterange field manually input wrong value should show toaster', async function (assert) {
assert.expect(5);
this.data.partner.fields.date_end = { string: 'Date End', type: 'date' };
this.data.partner.records[0].date_end = '2017-02-08';
const form = await createView({
View: FormView,
model: 'partner',
data: this.data,
arch: `
<form>
<field name="date" widget="daterange" options="{'related_end_date': 'date_end'}"/>
<field name="date_end" widget="daterange" options="{'related_start_date': 'date'}"/>
</form>`,
interceptsPropagate: {
call_service: function (ev) {
if (ev.data.service === 'notification') {
assert.strictEqual(ev.data.method, 'notify');
assert.strictEqual(ev.data.args[0].title, 'Invalid fields:');
assert.strictEqual(ev.data.args[0].message, '<ul><li>A date</li></ul>');
}
}
},
});
await testUtils.fields.editInput(form.$('.o_field_date_range:first'), 'blabla');
// click outside daterange field
await testUtils.dom.click(form.$el);
assert.hasClass(form.$('input[name=date]'), 'o_field_invalid',
"date field should be displayed as invalid");
// update input date with right value
await testUtils.fields.editInput(form.$('.o_field_date_range:first'), '02/08/2017');
assert.doesNotHaveClass(form.$('input[name=date]'), 'o_field_invalid',
"date field should not be displayed as invalid now");
// again enter wrong value and try to save should raise invalid fields value
await testUtils.fields.editInput(form.$('.o_field_date_range:first'), 'blabla');
await testUtils.form.clickSave(form);
form.destroy();
});
QUnit.module('FieldDate');
QUnit.test('date field: toggle datepicker [REQUIRE FOCUS]', async function (assert) {