From ae0253da40bb69dc97b4a4473338cca99a21a7e4 Mon Sep 17 00:00:00 2001 From: Benoit Socias Date: Fri, 21 Oct 2022 15:35:38 +0000 Subject: [PATCH] [FIX] web_editor, website: fix form custom fields starting with numbers Before this commit, following this flow: - Add a form - Add a selection field - Add an option starting with a number - Save => The created form's option value is only the number From the website_form options, the options HTMLElement of a form field are generated using the field's records ids for their html element value attribute (see FormEditor._renderField). When [1] refactored the website_form options to use a generic ListUserValueWidget, the list records id computation changed to use parseInt instead of strings. This commit changes the detection of parsable int to match only strings that are made only of digits, and less than 16 of them so that their value is not lost. [1]: https://github.com/odoo/odoo/commit/9304f8fa7bb94f21dd4caa83f9679ffd1642691b opw-2980760 closes odoo/odoo#108248 X-original-commit: 166f4aa4d23a01901585b60d69fac47fe70b7ebe Signed-off-by: Quentin Smetz (qsm) Signed-off-by: Benoit Socias (bso) Co-authored-by: Younn Olivier --- .../static/src/js/editor/snippets.options.js | 6 ++---- .../src/snippets/s_website_form/options.js | 6 ++---- .../static/tests/tours/website_form_editor.js | 17 +++++++++++++++++ .../tests/tours/website_form_editor_frontend.js | 7 ++++++- 4 files changed, 27 insertions(+), 9 deletions(-) diff --git a/addons/web_editor/static/src/js/editor/snippets.options.js b/addons/web_editor/static/src/js/editor/snippets.options.js index 1244773ff3a..60fe9f4072e 100644 --- a/addons/web_editor/static/src/js/editor/snippets.options.js +++ b/addons/web_editor/static/src/js/editor/snippets.options.js @@ -2146,9 +2146,8 @@ const ListUserValueWidget = UserValueWidget.extend({ if (this.el.dataset.idMode && this.el.dataset.idMode === "name") { id = el.name; } - const idInt = parseInt(id); return Object.assign({ - id: isNaN(idInt) ? id : idInt, + id: /^-?[0-9]{1,15}$/.test(id) ? parseInt(id) : id, name: el.value, display_name: el.value, }, el.dataset); @@ -2158,8 +2157,7 @@ const ListUserValueWidget = UserValueWidget.extend({ this.selected = checkboxes.map(el => { const input = el.parentElement.previousSibling.firstChild; const id = input.name || input.value; - const idInt = parseInt(id); - return isNaN(idInt) ? id : idInt; + return /^-?[0-9]{1,15}$/.test(id) ? parseInt(id) : id; }); values.forEach(v => { // Elements not toggleable are considered as always selected. diff --git a/addons/website/static/src/snippets/s_website_form/options.js b/addons/website/static/src/snippets/s_website_form/options.js index 52528e34b1d..db3f981dcdb 100644 --- a/addons/website/static/src/snippets/s_website_form/options.js +++ b/addons/website/static/src/snippets/s_website_form/options.js @@ -1303,8 +1303,7 @@ options.registry.WebsiteFieldEditor = FieldEditor.extend({ list.dataset.hasDefault = ['one2many', 'many2many'].includes(type) ? 'multiple' : 'unique'; const defaults = [...this.$target[0].querySelectorAll('[checked], [selected]')].map(el => { - const idInt = parseInt(el.value); - return isNaN(idInt) ? el.value : idInt; + return /^-?[0-9]{1,15}$/.test(el.value) ? parseInt(el.value) : el.value; }); list.dataset.defaults = JSON.stringify(defaults); @@ -1391,10 +1390,9 @@ options.registry.WebsiteFieldEditor = FieldEditor.extend({ options = [...multipleInputs.querySelectorAll('.checkbox input, .radio input')]; } return options.map(opt => { - const id = parseInt(opt.value); const name = select ? opt : opt.nextElementSibling; return { - id: isNaN(id) ? opt.value : id, + id: /^-?[0-9]{1,15}$/.test(opt.value) ? parseInt(opt.value) : opt.value, display_name: name.textContent.trim(), selected: select ? opt.selected : opt.checked, }; diff --git a/addons/website/static/tests/tours/website_form_editor.js b/addons/website/static/tests/tours/website_form_editor.js index ae256e4eb5f..9961d238707 100644 --- a/addons/website/static/tests/tours/website_form_editor.js +++ b/addons/website/static/tests/tours/website_form_editor.js @@ -271,6 +271,22 @@ odoo.define('website.tour.form_editor', function (require) { }, { content: "Remove Germany Option", trigger: '.o_we_select_remove_option:eq(0)', + }, { + content: "Click on Add new Checkbox", + trigger: 'we-list we-button.o_we_list_add_optional', + }, { + content: "Change last option label with a number", + trigger: 'we-list table input:eq(3)', + run: 'text 44 - UK', + }, { + content: "Check that the input value is the full option value", + trigger: 'we-list table input:eq(3)', + run: () => { + const addedOptionEl = document.querySelector('iframe.o_iframe').contentDocument.querySelector('.s_website_form_field select option[value="44 - UK"]'); + if (!addedOptionEl) { + console.error('The number option was not correctly added'); + } + }, }, { content: "Check the resulting snippet", trigger: "iframe .s_website_form_field.s_website_form_custom.s_website_form_required" + @@ -279,6 +295,7 @@ odoo.define('website.tour.form_editor', function (require) { ":has(.s_website_form_select_item:contains('Belgium'))" + ":has(.s_website_form_select_item:contains('France'))" + ":has(.s_website_form_select_item:contains('Canada'))" + + ":has(.s_website_form_select_item:contains('44 - UK'))" + ":not(:has(.s_website_form_select_item:contains('Germany')))", run: function () {}, }, diff --git a/addons/website/static/tests/tours/website_form_editor_frontend.js b/addons/website/static/tests/tours/website_form_editor_frontend.js index 4369b0249f2..179e343bf5f 100644 --- a/addons/website/static/tests/tours/website_form_editor_frontend.js +++ b/addons/website/static/tests/tours/website_form_editor_frontend.js @@ -137,6 +137,11 @@ tour.register("website_form_editor_tour_submit", { content: "Check if conditional field is visible, it should.", trigger: "input[name='email_cc']", }, + { + content: "Select state option", + trigger: "select[name='State']", + run: 'text 44 - UK', + }, { content: "Send the form", trigger: ".s_website_form_send" @@ -161,7 +166,7 @@ tour.register("website_form_editor_tour_results", { ['email_to', '=', 'test@test.test'], ['body_html', 'like', 'A useless message'], ['body_html', 'like', 'Service : Development Service'], - ['body_html', 'like', 'State : Belgium'], + ['body_html', 'like', 'State : 44 - UK'], ['body_html', 'like', 'Products : Xperia,Wiko Stairway'] ]], });