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

useForm setError typing seems wrong #2214

Open
axelvaindal opened this issue Jan 28, 2025 · 1 comment · May be fixed by #2224
Open

useForm setError typing seems wrong #2214

axelvaindal opened this issue Jan 28, 2025 · 1 comment · May be fixed by #2224
Labels
react Related to the react adapter

Comments

@axelvaindal
Copy link

Version:

  • @inertiajs/react version: latest

Describe the problem:

When using useForm in a react application, we have access to the function setError which has two overrides : one with a single error key and value, and another with an error object. The error object seems wrongly typed, because it assumes we always pass all key / value pair in the errors, which does not reflect the reality when used with validation libraries.

This is the current type 👇

errors: Record<keyof TForm, string>

It should probably be 👇

errors: Partial<Record<keyof TForm, string>>

In the same spirit, we currently create a lot of boilerplate code just to prevent sending the form when using validation librairies like zod/yup/joi etc. Would it be possible to consider accepting a resolver in the useForm hook configuration to prevent the sending of the form in case of validations error ? It would avoid unnecessary requests to the server and improve the DS similarly to what react-hook-form accepts.

Here is my current implementation with Yup as a reference 👇

function useValidatedForm<T extends Record<string, FormDataConvertible>>(schema: Schema, initialData?: T) {
  const form = useForm<T>(initialData);

  const isValid = useCallback(
    (data: T, schema: Schema) => {
      try {
        form.clearErrors();
        schema.validateSync(data, { abortEarly: false });
        return true;
      } catch (errors: any) {
        const validationErrors: Partial<Record<keyof T, string>> = {};

        errors.inner.forEach((error: ValidationError) => {
          if (error.path !== undefined) {
            validationErrors[error.path as keyof T] = error.errors[0];
          }
        });

        form.setError(validationErrors as Record<keyof T, string>);
        return false;
      }
    },
    [form],
  );

  const submit = useCallback(
    (method: Method, url: string, options?: VisitOptions) => {
      return form.submit(method, url, {
        ...options,
        onBefore: (visit) => {
          if (!isValid(form.data, schema)) {
            return false;
          }

          if (options?.onBefore) {
            return options.onBefore(visit);
          }
        },
      });
    },
    [form, schema, isValid],
  );

  return {
    ...form,
    submit,
  };
}

Steps to reproduce:

  • Call setError with an error object containing a single key/value pair -> TS errors
@axelvaindal axelvaindal added the react Related to the react adapter label Jan 28, 2025
@axelvaindal axelvaindal changed the title useForm setError useForm setError typing seems wrong Jan 28, 2025
@Verox001
Copy link

Verox001 commented Feb 1, 2025

Fixed in #2224. PR just has to be accepted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
react Related to the react adapter
Projects
None yet
2 participants