[FIX] website: prevent crash when missing field dependency in forms

Steps to reproduce:
- Add a form.
- Setup a conditional visibility on one field, using the "contains" or
  "does not contain" operator.
- Save.
- Open the HTML editor and voluntarily break the form by changing the
  "data-visibility-dependency" attribute to a random text (non existing
  field used as a dependency).
- Save => Crash on page load.

One customer apparently reached that case of receiving a `null` value
(actually possible when retrieving a form data which is an unchecked
checkbox for example) but combined with an operator which really
requires the received value to be a string... but not sure how this is
possible. In any case, it should not make the form crash: it's just a
problematic *visibility* dependency, it should not prevent using the
form. For now, let's prevent this case to crash and add an error in the
console so we may investigate the issue if it ever shows up in a test.

opw-3243345

closes odoo/odoo#125981

X-original-commit: 27144695ff4cd5fc026332c324cfdf8498d5ef14
Signed-off-by: Guillaume Dieleman (gdi) <gdi@odoo.com>
This commit is contained in:
qsm-odoo
2023-06-22 17:10:47 +02:00
parent a41e7d7ee5
commit 07ef90bcd6
@@ -658,10 +658,32 @@
* @returns {boolean}
*/
_compareTo(comparator, value = '', comparable, between) {
const valueWasNull = (value === null);
if (valueWasNull) {
// TODO One customer apparently reached that case of receiving
// a `null` value (actually possible when retrieving a form
// data which is an unchecked checkbox for example) but combined
// with an operator which really requires the received value to
// be a string... but not sure how this is possible. In any
// case, it should not make the form crash: it's just a
// problematic *visibility* dependency, it should not prevent
// using the form. So if the caller sends a `null` value, treat
// it as an empty string. For now, let's also add an error in
// the console so we may investigate the issue if it ever shows
// up in a test (grep: COMPARE_TO_INVALID_OPERATOR)
value = '';
}
switch (comparator) {
case 'contains':
if (valueWasNull) { // grep: COMPARE_TO_INVALID_OPERATOR
console.error("This field value is null but also uses an invalid operator");
}
return value.includes(comparable);
case '!contains':
if (valueWasNull) { // grep: COMPARE_TO_INVALID_OPERATOR
console.error("This field value is null but also uses an invalid operator");
}
return !value.includes(comparable);
case 'equal':
case 'selected':
@@ -687,6 +709,9 @@
return value.name === '';
}
// Date & Date Time comparison requires formatting the value
if (valueWasNull) { // grep: COMPARE_TO_INVALID_OPERATOR
console.error("This field value is null but also uses an invalid operator");
}
if (value.includes(':')) {
const datetimeFormat = time.getLangDatetimeFormat();
value = moment(value, datetimeFormat)._d.getTime() / 1000;