-
Notifications
You must be signed in to change notification settings - Fork 3.3k
fix(ui): password fields disappear after server-side validation fails #15092
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
Open
PatrikKozak
wants to merge
5
commits into
main
Choose a base branch
from
fix/password-fields-disappearing-on-validation-error
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+238
−62
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e116071
fix(ui): prevent setModified(false) on failed submissions to keep pas…
PatrikKozak fb654cd
Merge branch 'main' of github.com:payloadcms/payload into fix/passwor…
PatrikKozak 6f30f4c
fix(ui): prevent onChange from running when there are validation errors
PatrikKozak 5a7ed40
test: add additional check in test
PatrikKozak d21e5ac
fix(ui): prevent onChange from clearing server validation errors
PatrikKozak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| import type { FormState } from 'payload' | ||
|
|
||
| import { dequal } from 'dequal/lite' | ||
|
|
||
| /** | ||
| * Determines if the form state change is only due to error messages being added/removed, | ||
| * without any actual field value changes from user interaction. | ||
| * | ||
| * This is useful for detecting when server validation errors are added to the form state | ||
| * versus when a user actually modifies field values. | ||
| */ | ||
| export const isOnlyErrorStateChange = (prev: FormState, current: FormState): boolean => { | ||
| const prevWithoutErrors = Object.entries(prev).reduce((acc, [path, field]) => { | ||
| const { errorMessage, valid, ...rest } = field | ||
| acc[path] = rest | ||
| return acc | ||
| }, {} as FormState) | ||
|
|
||
| const currentWithoutErrors = Object.entries(current).reduce((acc, [path, field]) => { | ||
| const { errorMessage, valid, ...rest } = field | ||
| acc[path] = rest | ||
| return acc | ||
| }, {} as FormState) | ||
|
|
||
| return dequal(prevWithoutErrors, currentWithoutErrors) | ||
| } |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import { type CollectionConfig, ValidationError } from 'payload' | ||
|
|
||
| export const Users: CollectionConfig = { | ||
| slug: 'users', | ||
| admin: { | ||
| useAsTitle: 'email', | ||
| }, | ||
| auth: true, | ||
| fields: [], | ||
| hooks: { | ||
| beforeValidate: [ | ||
| (data) => { | ||
| const { password } = data.data || {} | ||
| // Only validate password length on update operations (when changing password) | ||
| // Skip validation on create to allow test suite initialization with "test" password | ||
| if (data.operation === 'update' && password && password.length < 8) { | ||
| throw new ValidationError({ | ||
| collection: 'users', | ||
| errors: [ | ||
| { | ||
| message: 'Password must be at least 8 characters long', | ||
| path: 'password', | ||
| }, | ||
| ], | ||
| }) | ||
| } | ||
| }, | ||
| ], | ||
| }, | ||
| } |
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
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.
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 like an unnecessary
elseblock considering L865 and L870 do the same thing.Wouldn't this simplified code work the same?