Skip to content

DataForm: support validation in date field type - #72048

Merged
oandregal merged 17 commits into
trunkfrom
add/validation-date-field
Oct 8, 2025
Merged

oandregal merged 17 commits into
trunkfrom
add/validation-date-field

Conversation

@jorgefilipecosta

@jorgefilipecosta jorgefilipecosta commented Oct 3, 2025 •

Copy link
Copy Markdown
Member

This PR implements validation on the date field. It supports both html5 validation and custom validation for business rules similar to other fields.
The validation works for both when picking a single date or a range of dates.
A story was added allowing us to test how validation works.

Screenshot

Screenshot 2025-10-03 at 12 14 54

Testing

Go to http://localhost:50240/?path=/story/dataviews-dataform--validation and test the date fields validation ( last 2 fields).

Notes for reviewers

Ignore whitespace otherwise it is harder to review https://github.com/WordPress/gutenberg/pull/72048/files?diff=unified&w=1.

@jorgefilipecosta jorgefilipecosta added [Feature] DataViews Work surrounding upgrading and evolving views in the site editor and beyond [Package] DataViews /packages/dataviews labels Oct 3, 2025
@github-actions

github-actions Bot commented Oct 3, 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: jorgefilipecosta <jorgefilipecosta@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.

@oandregal oandregal added the [Type] Enhancement A suggestion for improvement. label Oct 3, 2025
Comment on lines +717 to +719
data={ data }
field={ field }
setValue={ setValue }

@oandregal oandregal Oct 3, 2025 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is a bit too much. For example, setValue is already available via the field, and the value can be calculated via field.getValue as well. Can we improve this? Pass to this component (and the others) similar props as DataFormControlProps (embedding the onChange callbacks within), proper types (no any), etc.

Comment on lines +201 to +212
// Check HTML5 validity on all refs
const refs = Array.isArray( inputRefs ) ? inputRefs : [ inputRefs ];
for ( const ref of refs ) {
const input = ref.current;
if ( input && ! input.validity.valid ) {
setCustomValidity( {
type: 'invalid',
message: input.validationMessage,
} );
return;
}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is essentially relaying the native error message for the input date (required) and use it as custom validation, so we can position it when we want (below the calendar). Nice :)

@oandregal

Copy link
Copy Markdown
Member

This is great work, thanks @jorgefilipecosta !

It's working nicely for me, the only bit I'd like us to improve before merging is this #72048 (comment)

@oandregal
oandregal force-pushed the add/validation-date-field branch from 97d1887 to fe844f7 Compare October 8, 2025 11:55

@oandregal oandregal left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I've pushed the last adaptations in the internal controls. There are more things we can do (e.g., RelativeDateControl) but they're not essential to this PR. We can revisit later.

@oandregal
oandregal enabled auto-merge (squash) October 8, 2025 11:59
@github-actions

github-actions Bot commented Oct 8, 2025

Copy link
Copy Markdown

Flaky tests detected in 74cd149.
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/18344481155
📝 Reported issues:

@oandregal
oandregal merged commit 7fdd9d2 into trunk Oct 8, 2025
68 checks passed
@oandregal
oandregal deleted the add/validation-date-field branch October 8, 2025 12:59
@github-actions github-actions Bot added this to the Gutenberg 21.9 milestone Oct 8, 2025
sidharthpandita1 pushed a commit to sidharthpandita1/gutenberg that referenced this pull request Oct 10, 2025
Co-authored-by: jorgefilipecosta <jorgefilipecosta@git.wordpress.org>
Co-authored-by: oandregal <oandregal@git.wordpress.org>
{ customValidity && (
<p
className={ clsx(
'components-validated-control__indicator',

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I would prefer we not use external classes like this. It's very fragile and it will be strictly impossible once we move to CSS modules.

I have a question though — couldn't we just use the validation on the InputControl? Not doing it on the InputControl is actually detrimental because the input won't be semantically marked as invalid.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Hi @mirka, thank you for your insights!

I have a question though — couldn't we just use the validation on the InputControl? Not doing it on the InputControl is actually detrimental because the input won't be semantically marked as invalid.

Unfortunately I tried that, and never managed to make it work, when I changed dates on the calendar outside the the InputControl the validation error did not appeared or cleared. I think this is an issue that we may try to solve the inputs only validate errors coming from a user actions and not show anything when the error comes from a value change outside.
In terms of UX, we would have an input, validation error in the middle and then the calendar which is part of the input, that also does not seems ideal.

InputControl is actually detrimental because the input won't be semantically marked as invalid.

That's a good point I guess we could pass an error prop to the input to fix this?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I see! Then I think it's time to expose the components necessary for third-parties to build their own validated components. That was the plan to begin with, but we didn't have a use case yet. I can do some prep work first, and then we can try it on these date controls. How does that sound?

On the consumer side, I think the only prep work you will need is to wrap your compound date component into a single component that takes a label, onChange, and required prop. The system is supposed to work with any component that meets these requirements (theoretically 😄).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Hi @mirka, That sounds perfect, thank you a lot for your help here ☺️

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm working on a first step for this at #81574

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 [Package] DataViews /packages/dataviews [Type] Enhancement A suggestion for improvement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants