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(core): add getAllErrors to form api #1155

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

harry-whorlow
Copy link
Contributor

@harry-whorlow harry-whorlow commented Feb 18, 2025

Add's a utility function to form.api to return form errors combined with a reduced fieldMeta.errorMap.

example

form.getAllErrors() => {form: FormErrors, fields: Record<string, FieldErrors>}

Tasks

  • utility
  • tests

Copy link

nx-cloud bot commented Feb 18, 2025

View your CI Pipeline Execution ↗ for commit 51a9b8d.

Command Status Duration Result
nx affected --targets=test:sherif,test:knip,tes... ✅ Succeeded 1m 44s View ↗
nx run-many --target=build --exclude=examples/** ✅ Succeeded 31s View ↗

☁️ Nx Cloud last updated this comment at 2025-02-20 13:20:46 UTC

Copy link

pkg-pr-new bot commented Feb 18, 2025

Open in Stackblitz

More templates

@tanstack/angular-form

npm i https://pkg.pr.new/@tanstack/angular-form@1155

@tanstack/form-core

npm i https://pkg.pr.new/@tanstack/form-core@1155

@tanstack/react-form

npm i https://pkg.pr.new/@tanstack/react-form@1155

@tanstack/lit-form

npm i https://pkg.pr.new/@tanstack/lit-form@1155

@tanstack/solid-form

npm i https://pkg.pr.new/@tanstack/solid-form@1155

@tanstack/valibot-form-adapter

npm i https://pkg.pr.new/@tanstack/valibot-form-adapter@1155

@tanstack/vue-form

npm i https://pkg.pr.new/@tanstack/vue-form@1155

@tanstack/yup-form-adapter

npm i https://pkg.pr.new/@tanstack/yup-form-adapter@1155

@tanstack/zod-form-adapter

npm i https://pkg.pr.new/@tanstack/zod-form-adapter@1155

commit: 51a9b8d

Copy link

codecov bot commented Feb 18, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 88.00%. Comparing base (990a916) to head (51a9b8d).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1155      +/-   ##
==========================================
+ Coverage   87.86%   88.00%   +0.14%     
==========================================
  Files          30       30              
  Lines        1318     1334      +16     
  Branches      354      356       +2     
==========================================
+ Hits         1158     1174      +16     
  Misses        144      144              
  Partials       16       16              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@harry-whorlow harry-whorlow marked this pull request as ready for review February 18, 2025 21:53
Copy link
Member

@Balastrong Balastrong left a comment

Choose a reason for hiding this comment

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

Thanks for the great PR!

I think we're on the right direction but we can probably make it even better. Here I see you're returning the flat errors array for the form but the validationMap for the fields. We should probably return both for both.

What do you think about a return type similar to that:

{
  form: { errors: ValidationError[]; errorMap: ValidationErrorMap }
  fields: Record<
    DeepKeys<TFormData>,
    {
      errors: ValidationError[]
      errorMap: ValidationErrorMap
    }
  >
}

Feel free to make the types smarter if you think it's needed, the overall idea is to return both errors and errorMap for form and each field :)

return {
form: {
errors: this.state.errors,
errorMap: reduceErrorMap(this.state.errorMap as ValidationErrorMap),
Copy link
Contributor Author

@harry-whorlow harry-whorlow Feb 20, 2025

Choose a reason for hiding this comment

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

@Balastrong where is FormValidationErrorMap used as Record<ValidationErrorMapKeys, FormValidationError>?

Screenshot 2025-02-20 at 14 18 12

Im guessing when the form validation returns an object of validations. I don't feel particularly keen on leaving it as ValidationError | FormValidationError, Is there a utility type where this is narrowed?

Copy link
Member

Choose a reason for hiding this comment

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

Right, that type is gonna change a bit in #1104

In this case we should probably wait for the other PR to land first

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah, and increase the type complexity quite some too 😂

@juanvilladev
Copy link

Thanks for the great PR!

I think we're on the right direction but we can probably make it even better. Here I see you're returning the flat errors array for the form but the validationMap for the fields. We should probably return both for both.

What do you think about a return type similar to that:

{
  form: { errors: ValidationError[]; errorMap: ValidationErrorMap }
  fields: Record<
    DeepKeys<TFormData>,
    {
      errors: ValidationError[]
      errorMap: ValidationErrorMap
    }
  >
}

Feel free to make the types smarter if you think it's needed, the overall idea is to return both errors and errorMap for form and each field :)

This also somewhat ties into my PR where we need to determine which field errors came from form validations vs field validations. I think if we're getting all errors in a function like this, it'd be nice to know where the error came from as well. If this could be included within this functionally we could also use it internally to clear stale field errors sourced from the form.

@harry-whorlow
Copy link
Contributor Author

harry-whorlow commented Feb 21, 2025

This also somewhat ties into my PR where we need to determine which field errors came from form validations vs field validations. I think if we're getting all errors in a function like this, it'd be nice to know where the error came from as well. If this could be included within this functionally we could also use it internally to clear stale field errors sourced from the form.

you mean in the form validations?

It should work as form FormValidationError is a union of either:

const err = {
  onChange: ValidationError,
  onMount: ValidationError,
  //...
}

or

const err = {
  // name and age being fields in form
  onChange: { name: ValidationError, age: ValidationError },
  onMount: { name: ValidationError, age: ValidationError },
  //...
}
Screenshot 2025-02-21 at 08 34 01

So you will have a record of the errors and the field they come from at a form level.

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.

3 participants