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

Fix Display notification on each submit if form has errors #8057

Merged
merged 3 commits into from
Aug 10, 2022
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
2 changes: 1 addition & 1 deletion packages/ra-core/src/form/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export * from './useFormGroup';
export * from './useFormGroupContext';
export * from './useGetValidationErrorMessage';
export * from './useInitializeFormWithRecord';
export * from './useIsFormInvalid';
export * from './useNotifyIsFormInvalid';
export * from './useAugmentedForm';
export * from './useInput';
export * from './useSuggestions';
Expand Down
12 changes: 2 additions & 10 deletions packages/ra-core/src/form/useAugmentedForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import {
ValidateForm,
} from './getSimpleValidationResolver';
import { setSubmissionErrors } from './setSubmissionErrors';
import { useNotify } from '../notification';
import { useIsFormInvalid } from './useIsFormInvalid';
import { useNotifyIsFormInvalid } from './useNotifyIsFormInvalid';
import { useWarnWhenUnsavedChanges } from './useWarnWhenUnsavedChanges';

/**
Expand Down Expand Up @@ -88,13 +87,7 @@ export const useAugmentedForm = (props: UseAugmentedFormProps) => {
/* eslint-enable react-hooks/exhaustive-deps */

// notify on invalid form
const isInvalid = useIsFormInvalid(form.control);
const notify = useNotify();
useEffect(() => {
if (isInvalid) {
notify('ra.message.invalid_form', { type: 'warning' });
}
}, [isInvalid, notify]);
useNotifyIsFormInvalid(form.control);

// warn when unsaved change
useWarnWhenUnsavedChanges(
Expand Down Expand Up @@ -137,7 +130,6 @@ export const useAugmentedForm = (props: UseAugmentedFormProps) => {
form,
handleSubmit,
formHandleSubmit,
isInvalid,
};
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { useEffect, useState, useRef } from 'react';
import { useEffect, useRef } from 'react';
import { useFormState, Control } from 'react-hook-form';
import { useNotify } from '../notification';

/**
slax57 marked this conversation as resolved.
Show resolved Hide resolved
* This hook returns a boolean indicating whether the form is invalid.
* We use this to display an error message on submit in Form and SaveButton.
* This hook display an error message on submit in Form and SaveButton.
*
* We can't do the form validity check in the form submit handler
* as the form state may not have been updated yet when onSubmit validation mode is enabled
* or when the form hasn't been touched at all.
*/
export const useIsFormInvalid = (control?: Control) => {
const [isInvalid, setIsInvalid] = useState(false);
export const useNotifyIsFormInvalid = (control?: Control) => {
const { submitCount, errors } = useFormState(
control ? { control } : undefined
);
const submitCountRef = useRef(submitCount);
const notify = useNotify();

useEffect(() => {
// Checking the submit count allows us to only display the notification after users
Expand All @@ -23,12 +23,8 @@ export const useIsFormInvalid = (control?: Control) => {
submitCountRef.current = submitCount;

if (Object.keys(errors).length > 0) {
setIsInvalid(true);
} else {
setIsInvalid(false);
notify('ra.message.invalid_form', { type: 'warning' });
}
}
}, [errors, submitCount]);

return isInvalid;
}, [errors, submitCount, notify]);
};