Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle form submission errors that may be deeply nested in the return object #7821

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions awx/ui_next/src/components/FormField/FormSubmitError.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,26 @@ import React, { useState, useEffect } from 'react';
import { useFormikContext } from 'formik';
import { Alert } from '@patternfly/react-core';

const findErrorStrings = (obj, messages = []) => {
if (typeof obj === 'string') {
messages.push(obj);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

} else if (typeof obj === 'object') {
Object.keys(obj).forEach(key => {
const value = obj[key];
if (typeof value === 'string') {
messages.push(value);
} else if (Array.isArray(value)) {
value.forEach(arrValue => {
messages = findErrorStrings(arrValue, messages);
});
} else if (typeof value === 'object') {
messages = findErrorStrings(value, messages);
}
});
}
return messages;
};

function FormSubmitError({ error }) {
const [errorMessage, setErrorMessage] = useState(null);
const { setErrors } = useFormikContext();
Expand All @@ -18,14 +38,7 @@ function FormSubmitError({ error }) {
const errorMessages = error.response.data;
setErrors(errorMessages);

let messages = [];
Object.values(error.response.data).forEach(value => {
if (Array.isArray(value)) {
messages = messages.concat(value);
} else {
messages.push(value);
}
});
const messages = findErrorStrings(error.response.data);
setErrorMessage(messages.length > 0 ? messages : null);
} else {
/* eslint-disable-next-line no-console */
Expand Down
26 changes: 26 additions & 0 deletions awx/ui_next/src/components/FormField/FormSubmitError.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,30 @@ describe('<FormSubmitError>', () => {
expect(global.console.error).toHaveBeenCalledWith(error);
global.console = realConsole;
});

test('should display error message if field error is nested', async () => {
const error = {
response: {
data: {
name: 'There was an error with name',
inputs: {
url: 'Error with url',
},
},
},
};
let wrapper;
await act(async () => {
wrapper = mountWithContexts(
<Formik>{() => <FormSubmitError error={error} />}</Formik>
);
});
wrapper.update();
expect(
wrapper.find('Alert').contains(<div>There was an error with name</div>)
).toEqual(true);
expect(wrapper.find('Alert').contains(<div>Error with url</div>)).toEqual(
true
);
});
});