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
feat: add errors prop #11188
feat: add errors prop #11188
Conversation
Review or Edit in CodeSandboxOpen the branch in Web Editor • VS Code • Insiders |
Size Change: +65 B (0%) Total Size: 20.3 kB
|
Co-authored-by: Beier (Bill) <bluebill1049@hotmail.com>
Co-authored-by: Beier (Bill) <bluebill1049@hotmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome! thanks @kotarella1110
It seems that the second codesandbox link is dead : https://codesandbox.io/p/sandbox/github/react-hook-form/react-hook-form/tree/experiment/trigger-submit-on-pass/examples/server-actions |
Interesting. This version didn't work for my case. All error paths for fieldsets and field arrays should be appended with export const useExternalErrors = <T extends FieldValues>(
setError: UseFormSetError<T>,
errors?: FieldsValidationErrors<T>,
) => {
useEffect(() => {
if (!errors) return;
Object.entries(errors).forEach(([name, message]) => {
setError((name + '.root') as Path<T>, {
message: message as string,
type: 'backend',
});
});
}, [setError, errors]);
}; So |
Related Issue
CSB
Overview
This pull request introduces the addition of the
errors
prop, designed to dynamically respond to changes and update error states. This becomes particularly useful when your form needs to be synchronized with external states or server data. By incorporating this prop, you can seamlessly render error messages irrespective of JavaScript being enabled or disabled.Benefits
Furthermore, this prop serves as a convenient syntax sugar, replacing the
useEffect + setError
combination with the more conciseerrors
prop. Also it plays a crucial role in supporting Server Actions, allowing you to retrieve the errors returned by actions usingreact-dom
'suseFormState
. By passing these errors as theerrors
prop, you seamlessly integrate Server Actions' errors with React Hook Form's error handling.import { useFormState } from 'react-dom'; import { useForm } from 'react-hook-form'; const [formState, action] = useFormState(myAction, null); const { register, handleSubmit, formState: { errors }, } = useForm({ defaultValues: formState.values, + errors: formState.errors, progressive: true, });
This enhancement not only simplifies the codebase but also enhances the compatibility and cohesion between server-side and client-side error handling in React Hook Form.