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

feat: add errors prop #11188

Merged
merged 6 commits into from Nov 14, 2023
Merged

feat: add errors prop #11188

merged 6 commits into from Nov 14, 2023

Conversation

kotarella1110
Copy link
Member

@kotarella1110 kotarella1110 commented Nov 13, 2023

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 concise errors prop. Also it plays a crucial role in supporting Server Actions, allowing you to retrieve the errors returned by actions using react-dom's useFormState . By passing these errors as the errors 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.

Copy link

codesandbox bot commented Nov 13, 2023

Review or Edit in CodeSandbox

Open the branch in Web EditorVS CodeInsiders

Open Preview

Copy link
Contributor

github-actions bot commented Nov 13, 2023

Size Change: +65 B (0%)

Total Size: 20.3 kB

Filename Size Change
dist/index.cjs.js 10.1 kB +33 B (0%)
dist/index.umd.js 10.2 kB +32 B (0%)

compressed-size-action

@kotarella1110 kotarella1110 marked this pull request as ready for review November 13, 2023 13:45
src/useForm.ts Outdated Show resolved Hide resolved
kotarella1110 and others added 3 commits November 14, 2023 10:43
Co-authored-by: Beier (Bill) <bluebill1049@hotmail.com>
Co-authored-by: Beier (Bill) <bluebill1049@hotmail.com>
Copy link
Member

@bluebill1049 bluebill1049 left a comment

Choose a reason for hiding this comment

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

Awesome! thanks @kotarella1110

@bluebill1049 bluebill1049 merged commit c7b7eac into master Nov 14, 2023
7 checks passed
@bluebill1049 bluebill1049 deleted the feat/add-errors-prop branch November 14, 2023 04:46
@RemyMachado
Copy link

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

@ErBlack
Copy link

ErBlack commented Mar 15, 2024

Interesting. This version didn't work for my case.

All error paths for fieldsets and field arrays should be appended with .root. So, I decided to add .root to literally every error from the backend. And it works well with the setError function:

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 { 'name.root': {...} } displays near the name field when passed through setError and doesn't display when passed through the errors field.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants