From b463e2d1d3833598bc6059cb242983f9fbd2f452 Mon Sep 17 00:00:00 2001 From: Derek Leadbetter Date: Wed, 19 Jan 2022 12:26:07 -0500 Subject: [PATCH] FCA #115 - Adding "resolveValidationErrors" prop to EditContainer to allow custom-formed errors to be handled --- src/common/EditContainer.js | 51 ++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/src/common/EditContainer.js b/src/common/EditContainer.js index 09bbf2e7..e4d23858 100644 --- a/src/common/EditContainer.js +++ b/src/common/EditContainer.js @@ -13,7 +13,8 @@ type Props = { onInitialize?: (id: number) => Promise, onSave: (item: any) => Promise, required?: Array, - resolveValidationError?: ({ error: string, item: any, status: number, key: string }) => Array, + resolveValidationError?: ({ error: string, item: any, status: number, key: string }) => any, + resolveValidationErrors?: (errors: any) => any, validate?: (item: any) => Array }; @@ -160,30 +161,34 @@ const useEditContainer = (WrappedComponent: ComponentType) => ( onError({ response: { data: { errors = {} }, status } }: any) { const validationErrors = {}; - _.each(Object.keys(errors), (key) => { - const fieldErrors = errors[key]; - const value = this.state.item[key]; - - _.each(fieldErrors, (error) => { - if (error === ERROR_UNIQUE) { - _.extend(validationErrors, { [key]: i18n.t('EditContainer.errors.unique', { key, value }) }); - } else if (error === ERROR_EMPTY) { - _.extend(validationErrors, { [key]: i18n.t('EditContainer.errors.required', { key }) }); - } else if (this.props.resolveValidationError) { - _.extend(validationErrors, this.props.resolveValidationError({ - key, - error, - status, - item: this.state.item - })); - } + if (this.props.resolveValidationErrors) { + _.extend(validationErrors, this.props.resolveValidationErrors(errors)); + } else { + _.each(Object.keys(errors), (key) => { + const fieldErrors = errors[key]; + const value = this.state.item[key]; + + _.each(fieldErrors, (error) => { + if (error === ERROR_UNIQUE) { + _.extend(validationErrors, { [key]: i18n.t('EditContainer.errors.unique', { key, value }) }); + } else if (error === ERROR_EMPTY) { + _.extend(validationErrors, { [key]: i18n.t('EditContainer.errors.required', { key }) }); + } else if (this.props.resolveValidationError) { + _.extend(validationErrors, this.props.resolveValidationError({ + key, + error, + status, + item: this.state.item + })); + } + }); }); - }); - if (status === 400 && _.isEmpty(validationErrors)) { - _.extend(validationErrors, { error: i18n.t('EditContainer.errors.general') }); - } else if (status === 500 && _.isEmpty(validationErrors)) { - _.extend(validationErrors, { error: i18n.t('EditContainer.errors.system') }); + if (status === 400 && _.isEmpty(validationErrors)) { + _.extend(validationErrors, { error: i18n.t('EditContainer.errors.general') }); + } else if (status === 500 && _.isEmpty(validationErrors)) { + _.extend(validationErrors, { error: i18n.t('EditContainer.errors.system') }); + } } this.setState({ saving: false, validationErrors });