Skip to content

FormTokenField: Validate input before tokenizing on space - #69262

Closed
dhruvikpatel18 wants to merge 2 commits into
WordPress:trunkfrom
dhruvikpatel18:fix/form-token-field-validate-before-space-tokenize
Closed

dhruvikpatel18 wants to merge 2 commits into
WordPress:trunkfrom
dhruvikpatel18:fix/form-token-field-validate-before-space-tokenize

Conversation

@dhruvikpatel18

Copy link
Copy Markdown
Member

Closes #69252

What?

This PR fixes the FormTokenField component to properly validate input before creating tokens when pressing the space key.

Why?

Currently, when the tokenizeOnSpace prop is enabled, the FormTokenField component creates tokens immediately on space press without checking the experimentalValidateInput function. This can lead to invalid tokens being created, which is inconsistent with the validation behavior when using other methods of token creation (like pressing Enter or comma).

How?

Modified the space key handler in the onKeyDown function to:

  1. Check if there's valid input using inputHasValidValue()
  2. Call experimentalValidateInput with the current value
  3. Only create a token if both checks pass

@t-hamano t-hamano added [Type] Bug An existing feature does not function as intended [Package] Components /packages/components labels Feb 20, 2025
@youknowriad
youknowriad requested a review from Mamaduka February 20, 2025 23:50
@dhruvikpatel18
dhruvikpatel18 marked this pull request as ready for review February 21, 2025 04:46
@github-actions

github-actions Bot commented Feb 21, 2025 •

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: dhruvikpatel18 <dhruvik18@git.wordpress.org>
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
Co-authored-by: alshakero <alshakero@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@Mamaduka

Copy link
Copy Markdown
Member

Thank you, @dhruvikpatel18!

Does this also affect tokenization on the Ented key? Both are using addCurrentToken, and it seems the method has a handler for validation. Another unique key for this component is the comma, which uses a different callback - handleCommaKey.

Before proceeding with the fix, it would be a good idea to check the "records" and determine the reason for the differences.

@Mamaduka

Mamaduka commented Mar 6, 2025

Copy link
Copy Markdown
Member

Hi, @dhruvikpatel18

Are you still interested in continuing to work on this issue?

@dhruvikpatel18

Copy link
Copy Markdown
Member Author

Does this also affect tokenization on the Ented key? Both are using addCurrentToken, and it seems the method has a handler for validation. Another unique key for this component is the comma, which uses a different callback - handleCommaKey.

Before proceeding with the fix, it would be a good idea to check the "records" and determine the reason for the differences.

@Mamaduka Thank you for the feedback! You're right - I found that neither addCurrentToken() nor handleCommaKey() check the __experimentalValidateInput function. I will update my approach to fix validation at the function level rather than in the key handlers, ensuring consistent validation across Enter, Space, and comma tokenization methods.

@aduth

aduth commented Sep 8, 2026

Copy link
Copy Markdown
Member

Hey @dhruvikpatel18 , thanks for the contribution! I'm triaging some pull requests which are proposed to close issues which have already been resolved, and it appears that this one was addressed separately in #77181 . I'll close the pull request with the expectation that it may no longer be needed, but please feel free to follow-up (here, in the original issue, or in a new pull request) if there's more work to be done. Thanks!

@aduth aduth closed this Sep 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Package] Components /packages/components [Type] Bug An existing feature does not function as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FormTokenField does not call __experimentalValidateInput before tokenizing on space

4 participants