Skip to content

feat(form-core): Validation on first and consequent attempts#1286

Closed
theVedanta wants to merge 2 commits into
TanStack:mainfrom
theVedanta:validation-on-first-and-consequent-attempts
Closed

feat(form-core): Validation on first and consequent attempts#1286
theVedanta wants to merge 2 commits into
TanStack:mainfrom
theVedanta:validation-on-first-and-consequent-attempts

Conversation

@theVedanta

@theVedanta theVedanta commented Mar 15, 2025

Copy link
Copy Markdown
Contributor

Enables functionality like:

const form = useForm({
  defaultValues: {
    firstName: '',
    lastName: '',
  },
  validators: {
    onBlur: ({ value }) => {
      console.log('ON Blur:', value)
    },
    onMount: ({ value }) => {
      console.log('ON Mount:', value)
    },
  },
  validationOnFirstAttempt: 'blur',
  validationOnConsequentAttempts: 'mount',
})

This is in reference with issue #1272
All kinds of criticism, API-related edits, structural edits, etc. are welcome!

The current approach is quite verbose and adds an option in the formOptions that could be attributed to an object. However, since I don't know what we can attribute it to, keeping this style of options open.

Let me know if the functionality seems ok and we can work on documentation/writing tests.

@nx-cloud

nx-cloud Bot commented Mar 15, 2025

Copy link
Copy Markdown

View your CI Pipeline Execution ↗ for commit 9559bc1.

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

☁️ Nx Cloud last updated this comment at 2025-03-15 19:03:46 UTC

@pkg-pr-new

pkg-pr-new Bot commented Mar 15, 2025

Copy link
Copy Markdown

@codecov

codecov Bot commented Mar 15, 2025

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 88.83%. Comparing base (b0dc32c) to head (9559bc1).
Report is 83 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1286      +/-   ##
==========================================
+ Coverage   88.32%   88.83%   +0.51%     
==========================================
  Files          27       28       +1     
  Lines        1199     1272      +73     
  Branches      315      333      +18     
==========================================
+ Hits         1059     1130      +71     
- Misses        125      127       +2     
  Partials       15       15              

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Balastrong

Copy link
Copy Markdown
Member

Thanks for the PR!

However I'm not entirely sure this is the right direction. I see it quite confusing to define a submit validator and then a flag to have a different validator... only if it's the first attempt.

The current approach is quite verbose

What approach are we covering with this new api?

@theVedanta

Copy link
Copy Markdown
Contributor Author

Thanks for the PR!

However I'm not entirely sure this is the right direction. I see it quite confusing to define a submit validator and then a flag to have a different validator... only if it's the first attempt.

The current approach is quite verbose

What approach are we covering with this new api?

That's totally fair. I honestly think that checking submission attempts and defining a behaviour for validation would be quite verbose and this proves to be an alternative. React Hook Form has a similar API as mentioned in the issue referenced.

However, if you think that this kind of an option is not required in the tanstack-form, that would be understandable.

Comment on lines +1514 to +1515
? this.options.validationOnFirstAttempt ?? 'submit'
: this.options.validationOnConsequentAttempts ?? 'submit';

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

for me something like validationType and revalidationType sound more intuitive

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Alright I'll seek some approval about this and let you know!

@Balastrong Balastrong left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'd probably not move forward with this. I see value in adding those flags, but:

  • the same behaviour can be implemented in user-land with a single if
  • they're too specific on a single use case
  • if we allow for that, then we should add so many other flags and logic for everyone's individual needs which means paying a lot in maintenance on the long run

Thanks anyway, if we see more demand in the future we might think of a different API to handle that :)

@Balastrong Balastrong closed this Mar 28, 2025
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