From 467058731fa05bb20d666445ea9419d8c84a5b92 Mon Sep 17 00:00:00 2001 From: Nicolas Evrard Date: Wed, 5 Apr 2023 10:24:57 +0200 Subject: [PATCH] Return a compatible type for multi valued fields in unique_value [PREVIEW] --- src/common.js | 25 +++++++++++++------------ src/model.js | 27 ++++++++++++++++++++++----- 2 files changed, 35 insertions(+), 17 deletions(-) diff --git a/src/common.js b/src/common.js index 16693c82..91d77e11 100644 --- a/src/common.js +++ b/src/common.js @@ -2675,18 +2675,19 @@ unique_value: function(domain) { if ((domain instanceof Array) && (domain.length == 1)) { - domain = domain[0]; - var name = domain[0]; - var value = domain[2]; - var count = 0; - if (domain.length == 4 && name.endsWith('.id')) { - count = 1; - var model = domain[3]; - value = [model, value]; - } - if ((name.split('.').length - 1) == count && - (domain[1] == '=')) { - return [true, domain[1], value]; + let [name, operator, value, ...model] = domain[0]; + if (operator == '=' || + (operator == 'in' && value.length == 1)) { + value = operator == '=' ? value : value[0]; + var count = 0; + if (model.length && name.endsWith('.id')) { + count = 1; + model = model[0]; + value = [model, value]; + } + if ((name.split('.').length - 1) == count) { + return [true, name, value]; + } } } return [false, null, null]; diff --git a/src/model.js b/src/model.js index ff89cc17..ca213447 100644 --- a/src/model.js +++ b/src/model.js @@ -1672,7 +1672,10 @@ return true; } var invalid = false; - this.get_state_attrs(record).domain_readonly = false; + var state_attrs = this.get_state_attrs(record); + var is_required = Boolean(parseInt(state_attrs.required, 10)); + var is_invisible = Boolean(parseInt(state_attrs.invisible, 10)); + state_attrs.domain_readonly = false; var inversion = new Sao.common.DomainInversion(); var domain = inversion.simplify(this.validation_domains(record, pre_validate)); @@ -1688,11 +1691,20 @@ } else if (Sao.common.compare(domain, [['id', '=', null]])) { invalid = 'domain'; } else { + let [screen_domain, _] = this.get_domains( + record, pre_validate); var uniques = inversion.unique_value(domain); var unique = uniques[0]; var leftpart = uniques[1]; var value = uniques[2]; - if (unique) { + let unique_from_screen = inversion.unique_value( + screen_domain)[0]; + if (this._is_empty(record) && + !is_required && + !is_invisible && + !unique_from_screen) { + // Do nothing + } else if (unique) { // If the inverted domain is so constraint that only one // value is possible we should use it. But we must also pay // attention to the fact that the original domain might be @@ -1727,8 +1739,7 @@ } if (setdefault && !pre_validate) { this.set_client(record, value); - this.get_state_attrs(record).domain_readonly = - domain_readonly; + state_attrs.domain_readonly = domain_readonly; } } if (!inversion.eval_domain(domain, @@ -1736,7 +1747,7 @@ invalid = domain; } } - this.get_state_attrs(record).invalid = invalid; + state_attrs.invalid = invalid; return !invalid; } }); @@ -1779,6 +1790,12 @@ return value; }, set_client: function(record, value, force_change) { + if (value === null) { + value = []; + } + if (typeof(value) == 'string') { + value = [value]; + } if (value) { value = value.slice().sort(); }