Skip to content

Add async validation support to FormTokenField#71428

Closed
Shekhar0109 wants to merge 1 commit intoWordPress:trunkfrom
Shekhar0109:fix/async-form-validation
Closed

Add async validation support to FormTokenField#71428
Shekhar0109 wants to merge 1 commit intoWordPress:trunkfrom
Shekhar0109:fix/async-form-validation

Conversation

@Shekhar0109
Copy link
Contributor

What?

Closes #71310

This PR adds async validation support to the FormTokenField component. It updates the token submission logic to handle both synchronous and asynchronous validation functions. When async validation fails, an error message is shown below the input.

Why?

Currently, FormTokenField only supports synchronous validation. This prevents integration with APIs or other asynchronous validation logic. Supporting async validation allows token input to be verified in real-time against external sources before being added.

How?

  • Added isValidating and asyncError state to track validation state.
  • Converted addNewToken to an async function that handles both sync and async validation.
  • Display async validation error below the input using <StyledHelp> with a new .components-form-token-field__error CSS class.
  • Updated SCSS to style error messages in red.
  • Maintained existing synchronous validation fallback.

Testing Instructions

  1. Open Gutenberg and add a block or form that uses FormTokenField.
  2. Enter tokens that should pass and fail async validation.
  3. Press Enter, Tab, or blur the input to trigger async validation.
  4. Confirm that invalid tokens show the error message below the input.
  5. Confirm valid tokens are added as expected.

Testing Instructions for Keyboard

  • Navigate the tokens and suggestions using Tab, Shift+Tab, Enter, and arrow keys.
  • Ensure that async validation errors are accessible via screen readers.

@Shekhar0109 Shekhar0109 requested a review from ajitbohra as a code owner August 31, 2025 15:26
@github-actions
Copy link

github-actions bot commented Aug 31, 2025

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: Shekhar0109 <shekh0109@git.wordpress.org>
Co-authored-by: t-hamano <wildworks@git.wordpress.org>
Co-authored-by: mirka <0mirka00@git.wordpress.org>
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>

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

@github-actions github-actions bot added the First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository label Aug 31, 2025
@github-actions
Copy link

👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @Shekhar0109! 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.

@t-hamano t-hamano added [Type] Enhancement A suggestion for improvement. [Package] Components /packages/components labels Sep 1, 2025
@t-hamano
Copy link
Contributor

t-hamano commented Sep 1, 2025

Thanks for the PR!

If I understand correctly, #71310 seems to target the components in the validated-form-controls directory.

cc @WordPress/gutenberg-components @oandregal

@Shekhar0109
Copy link
Contributor Author

Thanks @t-hamano for clarifying! Yes, my intent was to address async validation in FormTokenField as part of the async refinements tracked in #71310. Please let me know if this should be scoped differently.

I noticed some failing checks:

I’ll update CHANGELOG.md for the components package.
I’ll run lint and unit tests locally to fix errors and add coverage for async validation.

I’ll push the fixes shortly 🚀

@mirka
Copy link
Member

mirka commented Sep 2, 2025

Validation support is being implemented in a more centralized way, and support for FormTokenField specifically is being worked on in #71350.

@Mamaduka
Copy link
Member

Mamaduka commented Sep 2, 2025

Thanks for contributing, @Shekhar0109!

It appears that the same task is already in progress, so I think we can close the PR.

I think you're a first-time contributor? You're welcome to contribute in whatever way you want, but general advice is to start with the 'Good First Issue' label that is usually more suited for first contributors - https://github.com/WordPress/gutenberg/issues?q=is%3Aissue%20state%3Aopen%20label%3A%22Good%20First%20Issue%22.

Don't forget to check the contribution guidelines - https://developer.wordpress.org/block-editor/contributors/code/. Configuring your development environment and following guidelines is important for an open-source project.

@Mamaduka Mamaduka added the [Status] Duplicate Used to indicate that a current issue matches an existing one and can be closed label Sep 2, 2025
@Mamaduka Mamaduka closed this Sep 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository [Package] Components /packages/components [Status] Duplicate Used to indicate that a current issue matches an existing one and can be closed [Type] Enhancement A suggestion for improvement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Validated form controls: Async refinements

4 participants