Skip to content

Conversation

@youknowriad
Copy link
Contributor

What?

This PR proposes a better Dev Experience when working with DataForm and validation. Instead of assuming the consumer is going to run 'isltemValid' manually and think about when and how to run validation properly, this PR runs validation on each change and passes the result as a second argument for the onChange callback.
This is kind of inline with how most form libraries work, they take care of the validation... and they only give you a state.

Testing Instructions

There shouldn't be any impact, the only place that uses validation right now is the "reoder page" action. You can test that action quickly or the validation story in storybook.

@youknowriad youknowriad self-assigned this Aug 26, 2025
@youknowriad youknowriad requested a review from oandregal as a code owner August 26, 2025 06:23
@github-actions
Copy link

github-actions bot commented Aug 26, 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: youknowriad <[email protected]>
Co-authored-by: oandregal <[email protected]>

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

@youknowriad youknowriad requested a review from gigitux August 26, 2025 06:24
@youknowriad youknowriad added [Feature] DataViews Work surrounding upgrading and evolving views in the site editor and beyond [Type] Enhancement A suggestion for improvement. labels Aug 26, 2025
@github-actions
Copy link

github-actions bot commented Aug 26, 2025

Flaky tests detected in 8b66d61.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/17231116306
📝 Reported issues:

export { VIEW_LAYOUTS } from './dataviews-layouts';
export { filterSortAndPaginate } from './filter-and-sort-data-view';
export type * from './types';
export { isItemValid } from './validation';
Copy link
Member

Choose a reason for hiding this comment

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

I'd like to leave this export for now: it can be useful in non-reactive situations (e.g., to validate the form data on load).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok works for me.

value: Record< string, any >,
validation: { isValid: boolean }
) => void;
applyChanges?: ( item: Item, changes: Partial< Item > ) => Item;
Copy link
Member

Choose a reason for hiding this comment

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

what's this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

An idea that I decided to ditch :P

@youknowriad youknowriad enabled auto-merge (squash) August 26, 2025 07:31
@youknowriad youknowriad merged commit 55d79f3 into trunk Aug 26, 2025
110 of 131 checks passed
@youknowriad youknowriad deleted the update/validation branch August 26, 2025 08:42
@github-actions github-actions bot added this to the Gutenberg 21.6 milestone Aug 26, 2025
oandregal added a commit that referenced this pull request Aug 26, 2025
@oandregal
Copy link
Member

oandregal commented Aug 26, 2025

@youknowriad while working on adding support for async validation, I just realized this PR is problematic:

  • We need to handle async validation, but we don't want to re-trigger onChange when we get the results of the validation (only when the content changes). I think we need to introduce a separate onValidate callback instead.
  • This is disconnected from the components, causing issues. For example, the 1st time, components do not trigger validation onChange, but onBlur. So, by doing this, we run into the following: the onChange callback will say a form is invalid while the component hasn't yet. By exposing an onValidate callback that is triggered by the leaf component onValidate we fix this as well.
  • This triggers validation twice: first, when the component validates; second, when the isItemValid is executed. With the goal of minimizing performance impact (e.g., triggering potentially time-consuming custom validations), we should look at triggering only once.

I'm working on addressing those in #71161. I'd like to revert this PR and focus on landing 71161 instead.

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

Labels

[Feature] DataViews Work surrounding upgrading and evolving views in the site editor and beyond [Type] Enhancement A suggestion for improvement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants