Repository navigation
Add Inline Error for Protected Post Password Field and Uncheck "Password Protected" for Empty Field - #67034
Add Inline Error for Protected Post Password Field and Uncheck "Password Protected" for Empty Field#67034sarthaknagoshe2002 wants to merge 6 commits into
Conversation
|
👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @sarthaknagoshe2002! In case you missed it, we'd love to have you join us in our Slack community. If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information. |
|
Thanks a lot @sarthaknagoshe2002! I tested:
Here's a string of 256 chars to test:
I think it handles the edge cases well too, e.g.,
I'll just add a couple of reviewers for sanity checks. |
| id={ passwordInputId } | ||
| __next40pxDefaultSize | ||
| __nextHasNoMarginBottom | ||
| maxLength={ 255 } |
There was a problem hiding this comment.
Should we keep this check and instead of an error show a warning that the pasted code has been trimmed?
Additionally we could also add this limitation to a help text.. 🤔
There was a problem hiding this comment.
If we keep that line, this condition won’t be triggered:
if ( newPassword.length > 255 ) {
setError( __( 'Password cannot exceed 255 characters.' ) );This means we won’t be able to check if the character limit is exceeded. To handle that, we would need to listen for keydown and paste events in order to show an error or warning when the limit is breached.
There was a problem hiding this comment.
I mean listening events won't be that straight-forward as this approach.
If your experience says that we should change the approach then lets do it 🚀.
Let me know accordingly 😇
There was a problem hiding this comment.
show a warning that the pasted code has been trimmed?
Just to make things clear, the error should be converted to warning & the message should say the following right?
Password cannot exceed 255 characters \n The pasted password may have been trimmed
There was a problem hiding this comment.
Yeah, I see the dilemma. It would be nice to keep the HTML attribute for compatibility. HTML validation attributes also work well with assistive technologies, improving accessibility.
There's another, low-tech option that might be better UX: just tell folks that the maximum is 255 before they enter, and reinstate the maxLenth attr.
Given that it's an edge case anyway, I don't think we need to overthink it.
Then we could just remove the error message and associated handlers.
What do folks think?
There was a problem hiding this comment.
I'm likely missing a nuance (apologies) but could it make sense to use browser native UI for this? Here's an example.
This seems like a good option, provided it doesn’t impact the validation process.
Another workaround could be displaying a warning at 255 characters, indicating that any additional characters will be trimmed off. This way we can keep the warning & the maxLenth attr too.
There was a problem hiding this comment.
This seems like a good option, provided it doesn’t impact the validation process.
Another workaround could be displaying a warning at 255 characters, indicating that any additional characters will be trimmed off. This way we can keep the warning & the
maxLenth attrtoo.
@ramonjd @ntsekouras @jameskoster awaiting your feedbacks on this suggestion.
There was a problem hiding this comment.
Keeping the max length attribute may lead to the existing problem, copying in a password 300 characters long would be trimmed to 255 but the error message would inform the user "any furtther characters will be trimmed" or whatever the string turns out to be.
The MDN recommendation is to code up validation in an accessible manner:
Browsers often don't let the user type a longer value than expected into text fields. A better user experience than just using maxlength is to also provide character count feedback in an accessible manner and let the user edit their content down to size. An example of this is the character limit when posting on social media. JavaScript, including solutions using maxlength, can be used to provide this.
-- MDN: client side form validation
There was a problem hiding this comment.
Then lets skip the max length attribute & just display an error message that the password can't exceed 255 chars
|
Since this PR, and correct me if I'm wrong @WordPress/gutenberg-components, there's a new input-validation-error system we can maybe use. Would that unblock this? |
Yes, we're currently moving some components around, but #81627 is something that could possibly be used. |
5217c6f to
bb942c3
Compare
|
Thanks for the heads up on the new component! I've just pushed an update that refactors the password field to use Let me know how this looks! |
|
The base branch is very old. The private version of |
bb942c3 to
8575465
Compare
| useEffect( () => { | ||
| if ( password && password.length > 255 ) { | ||
| passwordInputRef.current?.reportValidity(); | ||
| } | ||
| }, [ password ] ); |
There was a problem hiding this comment.
This effect is not needed. ValidatedInputControl already handles when the error should be shown.
There was a problem hiding this comment.
I couldn't tell whether you wanted any specific behavior to prevent users from leaving the form in an invalid state. But for example, you can do things like prevent the popover from closing when there's an error. Please see the Storybook docs for more examples.
There was a problem hiding this comment.
To explain the useEffect, without maxLength, ValidatedInputControl was only triggering its validation on blur (clicking outside the input but inside the dialog). The useEffect was added as a workaround to force the inline error to appear immediately as soon as the 255character limit was breached by typing or pasting.
Here's the working without that useEffect (The errors is only triggered after typing & clicking outside the input):
Screen.Recording.2026-08-29.at.6.05.28.PM.mov
And with the useEffect (The error appears on the limit breach):
Screen.Recording.2026-08-29.at.6.07.58.PM.mov
The immediate inline error prevented the user from navigating away without noticing the error, as they could otherwise paste a long password and click elsewhere, which would close the box. Previously, the error was only rendered after a click happened outside the input. In the latest commit, I fixed this issue by preventing the dialog box from closing until the password length is validated.
Following the Storybook docs you shared, I implemented the "Validate On Popover Close" pattern. If the field is invalid, the close is blocked and focus is pulled back to the input so the user isn't left stranded.
To restore that immediate visual feedback without overriding the component via reportValidity(), I also implemented the "Showing Errors Without Moving Focus" pattern from the docs. Dispatching a synthetic invalid event makes the error visible immediately upon limit breach, ensuring the user sees it before potentially unmounting the component by clicking the editor canvas. Check the updated PR description for the latest screencast.
There was a problem hiding this comment.
The useEffect was added as a workaround to force the inline error to appear immediately as soon as the 255character limit was breached by typing or pasting.
The default validation behavior is the behavior we have intentionally built into the design system as a default. The error isn't presented to the user until the first blur. Otherwise the experience can be needlessly stressful — being shown errors when you're not even finished typing. After the first blur, errors are shown/updated in real time.
We shouldn't override this default unless there's a clear reason to do so. Are there any reasons remaining, after the popover close guard is in place?
There was a problem hiding this comment.
This use case is somewhat unique. Blurring the password field usually means that the entire post status popup is being closed, you won't have a chance to see the error, it's not a typical form. The password change is submitted/committed only when the post is being saved. The post status popup itself doesn't have any OK/Cancel buttons.
For password fields specifically, it's quite common to validate them in real time, as you're typing, and unlike other types of fields, it doesn't feel annoying. WordPress itself has this UI when you're changing user password in WP Admin:
There was a problem hiding this comment.
- Option A — I think it's very strange to have a mini sub-form directly within a popup full of form elements.
- Option B — The counter is irrelevant to the vast majority of users, who will never get close to the limit. Also keeping the "last valid value" is not safe because the user would have no idea what that was. And given that we're dealing with a rare edge case here, I don't think we should build special handling to show counters when the user gets closer to the limit. But if we remove those aspects, it's pretty much the "prevent popover from closing when invalid" pattern I suggested initially.
- Option C — This is the modal dialog pattern I suggested. Though, we cannot have a checkbox suddenly trigger open a dialog, so I think the initial trigger would need to be a button, e.g. "Set password". Which perhaps changes to "Edit password" when set.
So I think it's either B or C, with B requiring the fewest changes to the experience, design, and code.
Any preferences from @WordPress/gutenberg-design?
There was a problem hiding this comment.
Is the question whether we should add new buttons or checkboxes in order to "allow you to save"?
This is conceptually a solved problem in form design. Buy a toaster online and type the wrong credit card number, press submit and it's scrolled to, red, and needing attention, often with preceeding text somewhere in a bulleted list above the form: "Invalid credit card", something to that effect.
The nuance here is that it's not a credit card form, it's just one element in a big and noisy inspector. What's our equivalent of the bulleted list of what you need to correct to save? Probably B or C as Lena suggested.
There was a problem hiding this comment.
it's pretty much the "prevent popover from closing when invalid" pattern I suggested initially.
Preventing the popover from closing is not so easy, there are some annoying details I described earlier that prevent it from working nicely. The user is stuck in an "invalid field" state that can't be cancelled. With a straightforward implementation, it will be actually possible to close the popover by unmounting the entire parent UI.
There was a problem hiding this comment.
C feels cleaner, B is the quickest (although a well-instructed agent could do decent job of implementing C with little effort).
There was a problem hiding this comment.
Thanks all. I went ahead with option C as suggested, the checkbox is replaced by a "Set password" / "Edit password" button that opens a dialog. The dialog edits a local draft and only commits a valid password, so the post entity never holds a value over 255 characters. That removes the save lock, the close guard, and the unmount edge case, and validation timing stays at the design system default. I've updated the PR description and added unit and e2e coverage.
I think we should also give some design inputs on whether the status toggle should hint that a password is set. But maybe this belongs to a different issue.
| @@ -239,7 +258,18 @@ export default function PostStatus() { | |||
| ) } | |||
| type="text" | |||
| id={ passwordInputId } | |||
| maxLength={ 255 } | |||
There was a problem hiding this comment.
Removing this means that values over 255 characters can get written to the entity. Save/Publish doesn't go through this form, so that value can get sent to the server, no? Is that fine? I agree that maxLength can be bad for UX, but then we prevent that value from reaching the server some other way.
There was a problem hiding this comment.
Good catch on the Save/Publish flow bypassing the popover.
Restoring maxLength would result in silent truncation, which defeats the original UX goal of #64157 (showing an inline warning without modifying the user's pasted input).
To prevent invalid data from reaching the server while preserving the warning, I think the correct approach would be to use the core/editor store's lockPostSaving / unlockPostSaving actions.
const { lockPostSaving, unlockPostSaving } = useDispatch( editorStore );
const isPasswordInvalid = password && password.length > 255;
useEffect( () => {
if ( isPasswordInvalid ) {
lockPostSaving( 'post-status-password-length' );
} else {
unlockPostSaving( 'post-status-password-length' );
}
return () => {
// Only release the lock on unmount if the password is valid.
// If the user clicks into the editor canvas and unmounts this tab,
// we must leave the post locked!
if ( ! isPasswordInvalid ) {
unlockPostSaving( 'post-status-password-length' );
}
};
}, [ isPasswordInvalid, lockPostSaving, unlockPostSaving ] );If the password exceeds 255 characters, the input will retain the full string, ValidatedInputControl will show the inline error, and the post's save/update button will be locked until the user shortens the input.
f9a3e2d to
2f7928a
Compare
🤖 PR meta 🤖🎉 PropsIf you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
Updated as activity occurs, without notifying anyone named here. Add the |
|
Thanks for all the iterations @sarthaknagoshe2002! Since #85081 the editor inspector renders the DataForm summary, so the |
# Conflicts: # packages/editor/CHANGELOG.md # packages/editor/src/components/post-status/index.jsx # tools/eslint/suppressions.json
|
Thanks @ntsekouras! I merged |

Fixes: #64157
What?
Replaces the "Password protected" checkbox and inline field in the Status & visibility popover with a "Set password" / "Edit password" button that opens a dialog.
Why?
Two problems:
Earlier iterations of this PR (inline error, popover close guard,
lockPostSaving) all ran into the same root problem: the entity could hold an invalid value while the user was still typing. Per the discussion with everyone in the threads below, this PR takes the modal approach.How?
ValidatedInputControlfrom@wordpress/ui. The dialog edits a local draft, and the entity is only updated when the user saves a valid password (not empty, 255 characters or fewer). Cancel, Esc, and a backdrop click discard the draft.showPasswordstate is gone.maxLength, nolockPostSaving, and no syntheticinvalidevents.Testing Instructions
Screenshots or screencast
password-modal.mov