Skip to content

Conversation

@jorgefilipecosta
Copy link
Member

Part of: #71500

This PR adds pattern based validation to all the fields that support it.

Some questions

When pattern validation fails the message is always: "Please match the requested format". Should we allows customizing this error message on the API?

Pattern on the api is pattern?: string | RegExp;. I think being able to provide a regex is nice, but in html it is always strings should we allow regex or force the consumer to pass a string?

cc: @oandregal

Testing Instructions

Execute npm run storybook:dev
Go to http://localhost:50240/?path=/story/dataviews-dataform--validation.
Verify the fields with pattern validation work as expected.

@github-actions
Copy link

github-actions bot commented Nov 11, 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: jorgefilipecosta <jorgefilipecosta@git.wordpress.org>
Co-authored-by: oandregal <oandregal@git.wordpress.org>
Co-authored-by: mcsf <mcsf@git.wordpress.org>

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

@jorgefilipecosta jorgefilipecosta added [Feature] DataViews Work surrounding upgrading and evolving views in the site editor and beyond [Type] Enhancement A suggestion for improvement. labels Nov 11, 2025
@jorgefilipecosta jorgefilipecosta changed the title Add/pattern validation Dataforms: Add pattern validation. Nov 11, 2025
@oandregal
Copy link
Member

When pattern validation fails the message is always: "Please match the requested format". Should we allows customizing this error message on the API?

Nah, it's good to start with opinionated messages. When/If we do this, any other existing rule also need it, so we may want to look at this separately.

@oandregal
Copy link
Member

Right now, the storybook contains a single example per control type, and the storybook controls allow testing them individually (or in combination). Could you 1) expose pattern as a storybook control and 2) update the existing controls that pattern (text, number, email, pattern, password) instead of creating new ones?

Screenshot 2025-11-13 at 19 54 31

);

// Convert pattern to string if it's a RegExp
let pattern;
Copy link
Member

Choose a reason for hiding this comment

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

In addition to per-control validation, the useFormValidity hook must return the same error in the validity prop and isValid false. This is demonstrated in the storybook by controlling the "save button" (it's disabled if the form is invalid).

@oandregal
Copy link
Member

Pattern on the api is pattern?: string | RegExp;. I think being able to provide a regex is nice, but in html it is always strings should we allow regex or force the consumer to pass a string?

We need strings for the fields to be serializable. Supporting Regexp objects is nice, I agree. It's fine by me to support both from the outset, and I don't see any issues. Though, I also wonder if they add anything really worth having that strings don't have (considering the serialization concerns). I'm going to summon @mcsf intuition about this.

@mcsf
Copy link
Contributor

mcsf commented Nov 14, 2025

We need strings for the fields to be serializable. Supporting Regexp objects is nice, I agree. It's fine by me to support both from the outset, and I don't see any issues. Though, I also wonder if they add anything really worth having that strings don't have (considering the serialization concerns). I'm going to summon @mcsf intuition about this.

This is not a strong opinion, but just offhand I can think of the fact that RegExp instances can carry flags, which are lost when extracting .source.

/^hello?$/i.source                        // "^hello?$"
/^hello?$/i.source === /^hello?$/g.source // true

Which sounds like a point of confusion that we can avoid by only accepting strings.

@jorgefilipecosta
Copy link
Member Author

We need strings for the fields to be serializable. Supporting Regexp objects is nice, I agree. It's fine by me to support both from the outset, and I don't see any issues. Though, I also wonder if they add anything really worth having that strings don't have (considering the serialization concerns). I'm going to summon @mcsf intuition about this.

This is not a strong opinion, but just offhand I can think of the fact that RegExp instances can carry flags, which are lost when extracting .source.

/^hello?$/i.source                        // "^hello?$"
/^hello?$/i.source === /^hello?$/g.source // true

Which sounds like a point of confusion that we can avoid by only accepting strings.

Good point @mcsf, I removed the RegExp possibility.

@jorgefilipecosta
Copy link
Member Author

Hi @oandregal, I applied your feedback and now on the storybook there is a control to enable pattern validation, the custom one should be set to none as together is hard to test.

@github-actions
Copy link

Flaky tests detected in aa0d102.
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/19508045220
📝 Reported issues:

Copy link
Member

@oandregal oandregal left a comment

Choose a reason for hiding this comment

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

Nice!

@jorgefilipecosta jorgefilipecosta merged commit af72678 into trunk Nov 21, 2025
34 checks passed
@jorgefilipecosta jorgefilipecosta deleted the add/pattern-validation branch November 21, 2025 14:17
@jorgefilipecosta
Copy link
Member Author

Thank you for the reviews @oandregal all the feedback was applied before the merge.

@github-actions github-actions bot added this to the Gutenberg 22.2 milestone Nov 21, 2025
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.

4 participants