diff options
Diffstat (limited to 'addons/web/static/src/js/fields')
| -rw-r--r-- | addons/web/static/src/js/fields/abstract_field.js | 10 | ||||
| -rw-r--r-- | addons/web/static/src/js/fields/relational_fields.js | 22 |
2 files changed, 30 insertions, 2 deletions
diff --git a/addons/web/static/src/js/fields/abstract_field.js b/addons/web/static/src/js/fields/abstract_field.js index 47802d5d8c7..6eac12a2dba 100644 --- a/addons/web/static/src/js/fields/abstract_field.js +++ b/addons/web/static/src/js/fields/abstract_field.js @@ -153,7 +153,7 @@ var AbstractField = Widget.extend({ // the 'required' flag is basically only needed to determine if the widget // is in a valid state (not valid if empty and required) - this.required = options.required || false; + this.required = options.required || this.field.required; // the 'idForLabel' is the (html) id that should be set to the relevent // dom entity. If done correctly, clicking on the corresponding label @@ -202,7 +202,13 @@ var AbstractField = Widget.extend({ * a field widget is valid if its value is valid and if there is a value when * it is required. This is checked before saving a record, by the form view. * - * @returns {boolean} + * Note: the return value may be a boolean. It is necessary for some fields + * (x2many) to sometimes ask confirmation to the user if the current dirty + * invalid line can be discarded. This is an asynchronous operation, so we + * need the expressivity of something like a deferred. + * + * @returns {boolean|Deferred} true/false if the widget is valid, or a + * deferred which resolves to true/false if the widget is valid or not */ isValid: function () { var is_required = this.required; diff --git a/addons/web/static/src/js/fields/relational_fields.js b/addons/web/static/src/js/fields/relational_fields.js index c29f958cc06..4ae30264ff0 100644 --- a/addons/web/static/src/js/fields/relational_fields.js +++ b/addons/web/static/src/js/fields/relational_fields.js @@ -560,6 +560,28 @@ var FieldX2Many = AbstractField.extend({ isSet: function () { return true; }, + /** + * This is a tricky override. For relational fields, we don't just need to + * know if the field is valid. We also need to discard some invalid lines, + * if the user is ok with it (or automatically if the invalid line is not + * dirty). This is the reason why we need to change the signature of the + * AbstractField isValid method to optionally return a deferred. + * + * @override + * @returns {boolean|Deferred} if there is a renderer (sub list or kanban + * view), we may need to ask the user => it will return a deferred. + * Otherwise, it returns a boolean + */ + isValid: function () { + if (!this.renderer) { + return this._super.apply(this, arguments); + } + var def = $.Deferred(); + this.renderer.canBeSaved() + .then(def.resolve.bind(def, true)) + .fail(def.resolve.bind(def, false)); + return def; + }, //-------------------------------------------------------------------------- // Private |
