Skip to content

Commit 4275147

Browse files
committed
[FIX] website: allow defaut value for data-for form field
Scenario: - go to the /contactus page - edit the subject field and set a default value then save Result: the default value is not saved. Reason: in 13.0, field default value had priority over data-for so an issue was solved by b637a5e that if you saved a form, the data-for would become the default for all the uses of that form (so eg. a job position would be set for all job positions). In 8d0a63f the priority was changed so data-for has higher priority than the default value, that made the prior fix unnecessary (just a nice to have, to not save the data-for as default that would be overridden by another data-for). The fix was reintroduced by ca433f3 but it removes default value if there is a data-for. Fix: remove the filling and removing of data-for value in edition: the data-for is not shown in the editor (allowing us to set default) but has the priority when rendering the field in non-editable mode. This is mirroring what is already done for data-fill-with. The added test steps, verify that the default value that is overridden by a data-for is kept for type/email/text shown inputs. Side note: this commit also remove useless data-fill-with="undefined" attributes that are added when editing field. They shouldn't cause any issue unless someone add a "undefined" field in the prefilled fields. opw-4794903 closes odoo#216642 X-original-commit: 33988b8 Signed-off-by: Outagant Mehdi (mou) <[email protected]> Signed-off-by: Nicolas Lempereur (nle) <[email protected]>
1 parent d024889 commit 4275147

File tree

4 files changed

+37
-32
lines changed

4 files changed

+37
-32
lines changed

addons/website/static/src/snippets/s_website_form/000.js

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,6 @@ import wUtils from '@website/js/utils';
2929
*/
3030
start: function () {
3131
if (this.editableMode) {
32-
// TODO: Improve this. Since the form behavior was handled using
33-
// two separate public widgets, and the "data-for" values were
34-
// removed (on destroy before saving), we still need to restore
35-
// them in edit mode in the case of a simple widget refresh.
36-
this.dataForValues = wUtils.getParsedDataFor(this.$target[0].id, this.$target[0].ownerDocument);
37-
for (const fieldEl of this._getDataForFields()) {
38-
if (!fieldEl.getAttribute("value")) {
39-
fieldEl.setAttribute("value", this.dataForValues[fieldEl.name]);
40-
}
41-
}
4232
// We do not initialize the datetime picker in edit mode but want the dates to be formated
4333
this.el.querySelectorAll('.s_website_form_input.datetimepicker-input').forEach(el => {
4434
const value = el.getAttribute('value');
@@ -53,21 +43,7 @@ import wUtils from '@website/js/utils';
5343
}
5444
return this._super(...arguments);
5545
},
56-
/**
57-
* @override
58-
*/
59-
destroy() {
60-
if (this.editableMode) {
61-
// The "data-for" values are always correctly added to the form
62-
// on the form widget start. But if we make any change to it in
63-
// "edit" mode, we need to be sure it will not be saved with
64-
// the new values.
65-
for (const fieldEl of this._getDataForFields()) {
66-
fieldEl.removeAttribute("value");
67-
}
68-
}
69-
this._super(...arguments);
70-
},
46+
// Todo: remove in master
7147
/**
7248
* @private
7349
*/

addons/website/static/src/snippets/s_website_form/options.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1581,7 +1581,7 @@ options.registry.WebsiteFieldEditor = FieldEditor.extend({
15811581
}
15821582
}
15831583
const newInputEl = this.$target[0].querySelector('input');
1584-
if (newInputEl) {
1584+
if (newInputEl && dataFillWith) {
15851585
newInputEl.dataset.fillWith = dataFillWith;
15861586
}
15871587
},

addons/website_hr_recruitment/static/tests/tours/website_hr_recruitment.js

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,20 @@
8888
document.querySelector('.o_iframe:not(.o_ignore_in_tour)').contentDocument.querySelector('input[name="job_id"]').value = 'FAKE_JOB_ID_DEFAULT_VAL';
8989
},
9090
}, {
91-
content: 'Edit the form',
92-
trigger: 'iframe input[type="file"]',
91+
content: 'Make the department_id field visible',
92+
trigger: "body",
93+
run: () => {
94+
const departmentEl = document.querySelector('.o_iframe:not(.o_ignore_in_tour)').contentDocument.querySelector('input[name="department_id"]');
95+
departmentEl.type = 'text';
96+
departmentEl.closest('.s_website_form_field').classList.remove('s_website_form_dnone');
97+
},
9398
}, {
94-
content: 'Add a new field',
95-
trigger: 'we-button[data-add-field]',
99+
content: 'Focus on department_id field',
100+
trigger: 'iframe input[name="department_id"]',
101+
}, {
102+
content: 'Add a fake default value for the department_id field',
103+
trigger: 'we-input[data-attribute-name="value"] input',
104+
run: 'text FAKE_DEPARTMENT_ID_DEFAULT_VAL',
96105
},
97106
...wTourUtils.clickOnSave(),
98107
{
@@ -117,19 +126,36 @@
117126
console.error('The job_id field has a wrong value');
118127
}
119128
}
129+
}, {
130+
content: 'Check that a department_id has been loaded',
131+
trigger: 'iframe input[name="department_id"][value="FAKE_DEPARTMENT_ID_DEFAULT_VAL"]',
132+
run: function () {
133+
if (this.$anchor.val() === "FAKE_DEPARTMENT_ID_DEFAULT_VAL") {
134+
console.error('The department_id data-for should have been applied');
135+
}
136+
}
120137
},
121138
...wTourUtils.clickOnEditAndWaitEditMode(),
122139
{
123-
content: 'Verify that the job_id field has kept its default value',
140+
content: 'Verify that the job_id hidden field has lost its default value',
124141
trigger: "body",
125142
run: () => {
126143
const doc = document.querySelector(".o_iframe:not(.o_ignore_in_tour)").contentDocument;
127144
const id = doc.querySelector('[data-oe-model="hr.job"]').dataset.oeId;
128145
if (!doc.querySelector(`input[name="job_id"][value="${id}"]`)) {
129-
console.error('The job_id field has lost its default value');
146+
console.error('The hidden field has kept its default value in edit mode instead of data-for');
130147
}
131148
}
132149
},
150+
{
151+
content: 'Verify that the department_id shown field has kept its default value',
152+
trigger: 'iframe input[name="department_id"][value="FAKE_DEPARTMENT_ID_DEFAULT_VAL"]',
153+
run: function () {
154+
if (this.$anchor.val() !== "FAKE_DEPARTMENT_ID_DEFAULT_VAL") {
155+
console.error('The department_id field has lost its default value');
156+
}
157+
},
158+
},
133159
]);
134160

135161
// This tour addresses an issue that occurred in a website form containing

addons/website_hr_recruitment/tests/test_website_hr_recruitment.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@
1111
@odoo.tests.tagged('post_install', '-at_install')
1212
class TestWebsiteHrRecruitmentForm(odoo.tests.HttpCase):
1313
def test_tour(self):
14+
department = self.env['hr.department'].create({'name': 'guru team'})
1415
job_guru = self.env['hr.job'].create({
1516
'name': 'Guru',
1617
'is_published': True,
18+
'department_id': department.id,
1719
})
1820
job_intern = self.env['hr.job'].create({
1921
'name': 'Internship',
2022
'is_published': True,
23+
'department_id': department.id,
2124
})
2225
self.start_tour(self.env['website'].get_client_action_url('/jobs'), 'model_required_field_should_have_action_name', login='admin')
2326

0 commit comments

Comments
 (0)