-
Notifications
You must be signed in to change notification settings - Fork 4.7k
MediaEdit: Support custom validation
#74704
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
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 If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
0e1253e to
1cf9669
Compare
| ( Array.isArray( value ) && !! value.length ) || value | ||
| ? 'v' // We don't care about the actual value, just that it exists. | ||
| : '' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This means the input will be invalid when:
- value is an empty array
- value is a scalar but falsy (undefined, null, empty, false, 0, NaN)
This logic is different from the type, that checks for null/undefined/empty. Shouldn't they be the same?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're not wrong about this and after thinking more I followed a different approach. MediaEdit fields can have different values and validation logic (array for multiple, 0 that implies empty for FeaturedImage etc..), so it makes more sense for any field that uses it and wants validation, to add a custom validation function. I'll update the PR's description with some test instructions.
The new approach is to have an input and programmatically setCustomValidity based on the validity object.
21309f9 to
9ff04ba
Compare
|
Flaky tests detected in 9ff04ba. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/21165134775
|
required validationcustom validation
Yes, this is how validation works. Validation is intentionally designed as controlled state: see PR and discussion on alternative design. There were some challenges when it was not. |
oandregal
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for making media-edit support validation!
Co-authored-by: ntsekouras <ntsekouras@git.wordpress.org> Co-authored-by: oandregal <oandregal@git.wordpress.org>
What?
Part of: #72614
This PR adds support for
customvalidation in MediaEdit component. The code is very similar to thedatecontrol with custom handling because there are multiple interactive controls inside and we don't have a simpleinputcontrol.Since MediaEdit has various complex components without an
input, I used a hidden input field and programmaticallysetCustomValiditybased on the validity object (this is used for prevent form submissions).This also means that in order for MediaEdit to display validation messages, the consumer should also pass the
validityobject inDataForm. Is this expected and/or reasonable @oandregal?Testing Instructions
You can test it by updating the featured image field like this:
You should also update the post edit form to pass the
validityobject.Video with the snippets above applied.
Noting that in trunk we don't have a validation requirement for featured image field, so no validation message is being displayed.
Screen.Recording.2026-01-20.at.1.30.35.PM.mov