From 144e17e5d22329f192ab7497ab9f0a3d7f4f4aa0 Mon Sep 17 00:00:00 2001 From: VAN BOSSUYT Nicolas Date: Fri, 4 Aug 2023 13:07:45 +0000 Subject: [PATCH] [FIX] website: prevent date input to fallback on today's date The cause of the issue is that `datetimepicker('viewDate')` will return the datetime of today by default event if the user selects nothing, to remedy this, we now check if the user has chosen something before storing the content of the field in `form_values`. Step to reproduce: - Install website_crm_partner_assign to have the "Partnership Date" field on res.partner. - Install website_sale to have the "Create customer" capability on the website forms. - Drag & drop a form snippet on any page - Select "Create customer" as form action - Add a custom field "Partnership Date" - Submit the form without filling the Partnership Date field - The field will be set to today's date while it should have been left void. opw-3333364 closes odoo/odoo#131614 X-original-commit: 0a5668d36cc43cf346c5aecb9236d690a6694a45 Signed-off-by: Romain Derie (rde) --- .../website/static/src/snippets/s_website_form/000.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/addons/website/static/src/snippets/s_website_form/000.js b/addons/website/static/src/snippets/s_website_form/000.js index 9411761857d..119d2a4f5cb 100644 --- a/addons/website/static/src/snippets/s_website_form/000.js +++ b/addons/website/static/src/snippets/s_website_form/000.js @@ -336,13 +336,21 @@ // force server date format usage for existing fields this.$el.find('.s_website_form_field:not(.s_website_form_custom)') .find('.s_website_form_date, .s_website_form_datetime').each(function () { + const inputEl = this.querySelector('input'); + + // Datetimepicker('viewDate') will return `new Date()` if the + // input is empty but we want to keep the empty value + if (!inputEl.value) { + return; + } + var date = $(this).datetimepicker('viewDate').clone().locale('en'); var format = 'YYYY-MM-DD'; if ($(this).hasClass('s_website_form_datetime')) { date = date.utc(); format = 'YYYY-MM-DD HH:mm:ss'; } - form_values[$(this).find('input').attr('name')] = date.format(format); + form_values[inputEl.getAttribute('name')] = date.format(format); }); if (this._recaptchaLoaded) {