-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
perf: only validate filter options on submit #10738
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
eb194d6
to
61f3010
Compare
DanRibbens
approved these changes
Jan 23, 2025
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.
Looks good!
As we discussed, make sure autosave still works as intended with this new feature and add an e2e test or two and merge.
jacobsfletch
added a commit
that referenced
this pull request
Jan 27, 2025
Field validations can be expensive, especially custom validations that are async or highly complex. This can lead to slow form state response times when generating form state for many such fields. Ideally, we only run validations on fields whose values have changed. This is not possible, however, because field validation functions might reference _other_ field values with their args, and there is no good way of detecting exactly which fields should run in this case. The next best thing here is to only run validations _after the form has been submitted_, and then every `onChange` event thereafter until a successful submit has taken place. This is an elegant solution because we currently don't _render_ field errors until submission anyway. This change will significantly speed up form state response times, at least until the form has been submitted. From then on, all field validations will run regardless, just as they do now. If custom validations continue to slow down form state response times, there is a new `event` arg introduced in #10738 that can be used to control whether heavy operations occur on change or on submit. Related: #10638
🚀 This is included in version v3.20.0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Field validations currently run very often, such as within form state on type. This can lead to serious performance implications within the admin panel if those validation functions are async, especially if they perform expensive database queries. One glaring example of this is how all relationship and upload fields perform a database lookup in order to evaluate that the given value(s) satisfy the defined filter options. If the field is polymorphic, this can happen multiple times over, one for each collection. Similarly, custom validation functions might also perform expensive tasks, something that Payload has no control over.
The fix here is two-fold. First, we now provide a new
event
arg to allvalidate
functions that allow you to opt-in to performing expensive operations only when documents are submitted, and fallback to significantly more performant validations as form state is generated. This new pattern will be the new default for relationship and upload fields, however, any custom validation functions will need to be implemented in this way in order to take advantage of it. Here's what that might look like:The second part of this is to only run validations after the form as been submitted, and then every change event thereafter. This work is being done in #10580.