-
-
Notifications
You must be signed in to change notification settings - Fork 555
feat(form-core): add onDynamicListenTo validator option #1896
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
base: main
Are you sure you want to change the base?
Conversation
- Add onDynamicListenTo to FieldValidators interface - Implement dynamic cause handling in getLinkedFields method - Add password/confirmPassword example demonstrating cross-field validation
|
|
View your CI Pipeline Execution ↗ for commit 5dbb044
☁️ Nx Cloud last updated this comment at |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1896 +/- ##
==========================================
- Coverage 90.35% 89.64% -0.71%
==========================================
Files 38 48 +10
Lines 1752 1951 +199
Branches 444 493 +49
==========================================
+ Hits 1583 1749 +166
- Misses 149 181 +32
- Partials 20 21 +1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
- Add test for onDynamic cross-field validation with onDynamicListenTo - Add test for onDynamicAsync cross-field validation with onDynamicListenTo - Fix getLinkedFields to return validator cause for each linked field - Add 'dynamic' case to defaultValidationLogic - Clear dynamic errors when running change/blur validation
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.
I like the idea, but the implementation needs some adjustment. There's a chance the issues come from a core problem with onDynamic, but it's not clear from this diff alone.
Missing changes
There's also Addressed in 89adf3aFieldGroupApi.getFormFieldOptions that needs to include onDynamicListenTo. Follow the onChangeListenTo / onBlurListenTo lines in there as reference.
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.
I'm not sure if adding this to the simple example makes sense, since
- It's not a simple implementation
- It's unrelated to the other fields
Having linked fields as examples is a good idea though. Maybe it could be its own example? Perhaps it can be added to large-form instead. I'll leave it up to you.
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.
I believe it makes more sense to include it in large-form rather than as a separate example.
Linked field validation is a pattern commonly used in real-world scenarios, so it fits naturally within that example.
I'll move it to large-form. Is that okay with you?
packages/form-core/src/FieldApi.ts
Outdated
| const dynamicErrKey = getErrorMapKey('dynamic') | ||
| if ( | ||
| this.state.meta.errorMap[dynamicErrKey] && | ||
| this.state.meta.errorSourceMap[dynamicErrKey] === 'field' | ||
| ) { | ||
| this.setMeta((prev) => ({ | ||
| ...prev, | ||
| errorMap: { | ||
| ...prev.errorMap, | ||
| [dynamicErrKey]: undefined, | ||
| }, | ||
| errorSourceMap: { | ||
| ...prev.errorSourceMap, | ||
| [dynamicErrKey]: undefined, | ||
| }, | ||
| })) | ||
| } |
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.
This seems out of place for setValue. Why does onDynamic need special behaviour in this case?
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.
You're right
I was trying to clear dynamic errors before change validation runs, but if validate('change') passes, it should naturally clear all errors anyway.
| case 'dynamic': { | ||
| // Run dynamic, server validation | ||
| return props.runValidation({ | ||
| validators: [onDynamicValidator, onServerValidator], | ||
| form: props.form, | ||
| }) | ||
| } |
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.
I assumed the point of onDynamic was that it would be dictated by validationLogic. However, it now needs to be addressed in revalidateLogic. I feel like there's some underlying issue that should be fixed, but I'll need to look at it myself to know for sure.
| let resolve!: () => void | ||
| let promise = new Promise((r) => { | ||
| resolve = r as never | ||
| }) |
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.
This on-demand resolving is very difficult to follow in this unit test due to how hacky it is.
I think an implementation with vi.useFakeTimers and a setTimeout + vi.awaitAllTimersAsync is way clearer in terms of code flow.
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.
I'll refactor to use vi.useFakeTimers() with setTimeout + vi.awaitAllTimersAsync() as you suggested. Much clearer.
Adds support for onDynamicListenTo to enable cross-field dynamic validation.
Changes
onDynamicListenTotoFieldValidatorsinterfacegetLinkedFieldsmethodFixes #1698