Skip to content

Try/async validation#68

Open
dinhtungdu wants to merge 1 commit intofeat/lazy-load-elementsfrom
try/async-validation
Open

Try/async validation#68
dinhtungdu wants to merge 1 commit intofeat/lazy-load-elementsfrom
try/async-validation

Conversation

@dinhtungdu
Copy link
Copy Markdown
Owner

What?

Closes

Why?

How?

Testing Instructions

Testing Instructions for Keyboard

Screenshots or screencast

Before After

@github-actions
Copy link
Copy Markdown

github-actions bot commented Oct 13, 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: dinhtungdu <dinhtungdu@git.wordpress.org>
Co-authored-by: oandregal <oandregal@git.wordpress.org>

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

Comment on lines +731 to +737
### `isItemValidAsync`

`isItemValidAsync` provides the same validation as `isItemValid`, but awaits any asynchronous `elements` sources that resolve to selectable options. Use this helper when your field definitions provide `Promise` or function-based `elements`. It returns a `Promise<boolean>` that resolves once all relevant element sources have been processed.

### `useAsyncValidation`

`useAsyncValidation( item, fields, form )` is a React hook that performs the same asynchronous validation as `isItemValidAsync` and returns an object with `{ isValid, isResolving, validate }`. The `validate` function can be called to trigger validation manually, while `isResolving` indicates when validation is in progress.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we want to consolidate everything into a single API, not having three slightly different functions.

Copy link
Copy Markdown
Owner Author

@dinhtungdu dinhtungdu Oct 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want a single API and keep the current integration working, then the only way is probably to move the fetch to normalizeFields, but we'll end up with limited validation that only validates against fetched elements. It's not bad actually.

}

try {
const elements = await resolveFieldElements( field.elements );
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fetching elements on mount. We discussed this here, where I suggested having a single place (normalizeFields?). But there's now 3 places where we have this logic: useFieldElements, resolveFieldElements, resolveFieldElements.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, both useFieldElements and resolveFieldsElements call resolveFieldElements in their body. But I agree that it's getting complex. The only concern with moving to normalizeFields for me is that we still have to make more requests for pagination and search, and we probably end up fetching in 2 places.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants