From 0c35d43adfbb41e6008d35d12b2dbd5ac952a3c0 Mon Sep 17 00:00:00 2001 From: "Xavier BOL (xbo)" Date: Tue, 4 Oct 2022 09:35:36 +0200 Subject: [PATCH] [FIX] web: check if record in component props is defined since it is optional Before this commit, when a component using the `useInputField` hook (for instance `TextField`) and the `record` is not given in its props, the hook will crash since it assume the `record` is always defined. However, the props definition the `record` is optional. This commit checks if `record` is defined in the component before using it to be sure to avoid any crash because it is not defined. task-2947516 X-original-commit: ebe3d00e52dd6097ce55986bc9e3bf9d79ca64cc --- addons/web/static/src/views/fields/input_field_hook.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/addons/web/static/src/views/fields/input_field_hook.js b/addons/web/static/src/views/fields/input_field_hook.js index 7fe73b57540..2c7119015f6 100644 --- a/addons/web/static/src/views/fields/input_field_hook.js +++ b/addons/web/static/src/views/fields/input_field_hook.js @@ -56,7 +56,9 @@ export function useInputField(params) { try { val = params.parse(val); } catch (_e) { - component.props.record.setInvalidField(component.props.name); + if (component.props.record) { + component.props.record.setInvalidField(component.props.name); + } isInvalid = true; } } @@ -101,7 +103,7 @@ export function useInputField(params) { * If it is not such a case, we update the field with the new value. */ useEffect(() => { - const isInvalid = component.props.record.isInvalid(component.props.name); + const isInvalid = component.props.record ? component.props.record.isInvalid(component.props.name) : false; if (inputRef.el && !isDirty && !isInvalid) { inputRef.el.value = params.getValue(); lastSetValue = inputRef.el.value; @@ -133,7 +135,7 @@ export function useInputField(params) { isInvalid = true; if (urgent) { return; - } else { + } else if (component.props.record) { component.props.record.setInvalidField(component.props.name); } }