From 9ed60368613ced4bfebe82f990b2b65c5fa5b71e Mon Sep 17 00:00:00 2001 From: Aaron Bohy Date: Mon, 31 Jul 2023 08:16:35 +0000 Subject: [PATCH] [REF] web: RelationalModel: add option to display invalid notif This commit removes a function from the Record datapoint, and replaces it by an option to checkValidity, as we want Record to have a few public methods as possible. Part of task~3179751 closes odoo/odoo#130170 Signed-off-by: Jorge Pinna Puissant (jpp) --- .../src/model/relational_model/record.js | 39 +++++++++---------- .../src/views/fields/relational_utils.js | 3 +- 2 files changed, 19 insertions(+), 23 deletions(-) diff --git a/addons/web/static/src/model/relational_model/record.js b/addons/web/static/src/model/relational_model/record.js index c7eff2f4520..1a3ab93380c 100644 --- a/addons/web/static/src/model/relational_model/record.js +++ b/addons/web/static/src/model/relational_model/record.js @@ -133,11 +133,11 @@ export class Record extends DataPoint { return this.model.mutex.exec(() => this._toggleArchive(true)); } - async checkValidity() { + async checkValidity({ displayNotification } = {}) { if (!this._urgentSave) { await this.model._askChanges(); } - return this._checkValidity(); + return this._checkValidity({ displayNotification }); } delete() { @@ -208,21 +208,6 @@ export class Record extends DataPoint { return this.model.mutex.exec(() => this._load()); } - openInvalidFieldsNotification() { - if (this._invalidFields.size) { - const items = [...this._invalidFields].map((fieldName) => { - return `
  • ${escape(this.fields[fieldName].string || fieldName)}
  • `; - }, this); - this._closeInvalidFieldsNotification = this.model.notification.add( - markup(``), - { - title: _t("Invalid fields: "), - type: "danger", - } - ); - } - } - async save(options) { await this.model._askChanges(); return this.model.mutex.exec(() => this._save(options)); @@ -321,7 +306,7 @@ export class Record extends DataPoint { this._setEvalContext(); } - _checkValidity({ silent } = {}) { + _checkValidity({ silent, displayNotification } = {}) { const unsetRequiredFields = []; for (const fieldName in this.activeFields) { const fieldType = this.fields[fieldName].type; @@ -364,7 +349,20 @@ export class Record extends DataPoint { this._unsetRequiredFields.add(fieldName); this._setInvalidField(fieldName); } - return !this._invalidFields.size; + const isValid = !this._invalidFields.size; + if (!isValid && displayNotification) { + const items = [...this._invalidFields].map((fieldName) => { + return `
  • ${escape(this.fields[fieldName].string || fieldName)}
  • `; + }, this); + this._closeInvalidFieldsNotification = this.model.notification.add( + markup(``), + { + title: _t("Invalid fields: "), + type: "danger", + } + ); + } + return isValid; } _computeDataContext() { @@ -791,8 +789,7 @@ export class Record extends DataPoint { this.data[fieldName]._abandonRecords(); } } - if (!this._checkValidity()) { - this.openInvalidFieldsNotification(); + if (!this._checkValidity({ displayNotification: true })) { return false; } const changes = this._getChanges(); diff --git a/addons/web/static/src/views/fields/relational_utils.js b/addons/web/static/src/views/fields/relational_utils.js index aec6957a68a..ded01cd476d 100644 --- a/addons/web/static/src/views/fields/relational_utils.js +++ b/addons/web/static/src/views/fields/relational_utils.js @@ -590,7 +590,7 @@ export class X2ManyFieldDialog extends Component { async save({ saveAndNew }) { const disabledButtons = this.disableButtons(); - if (await this.record.checkValidity()) { + if (await this.record.checkValidity({ displayNotification: true })) { try { await this.props.save(this.record); } catch (error) { @@ -601,7 +601,6 @@ export class X2ManyFieldDialog extends Component { this.record = await this.props.addNew(); } } else { - this.record.openInvalidFieldsNotification(); this.enableButtons(disabledButtons); return false; }