Skip to content

Conversation

@ntsekouras
Copy link
Contributor

@ntsekouras ntsekouras commented Jan 16, 2026

What?

Part of: #72614

This PR adds support for custom validation in MediaEdit component. The code is very similar to the date control with custom handling because there are multiple interactive controls inside and we don't have a simple input control.

Since MediaEdit has various complex components without an input, I used a hidden input field and programmatically setCustomValidity based 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 validity object in DataForm. Is this expected and/or reasonable @oandregal?

Testing Instructions

You can test it by updating the featured image field like this:

function validateRequired(
	item: BasePostWithEmbeddedFeaturedMedia,
	field: NormalizedField< BasePostWithEmbeddedFeaturedMedia >
): string | null {
	const value = field.getValue( { item } );
	if ( value === 6 ) {
		return 'This specific image is not allowed.';
	}
	return ! [ 0, undefined, '', null ].includes( value )
		? null
		: __( 'This field is required.' );
}
const featuredImageField: Field< BasePostWithEmbeddedFeaturedMedia > = {
	id: 'featured_media',
	type: 'media',
	label: __( 'Featured Image' ),
	Edit: MediaEdit,
	render: FeaturedImageView,
	setValue: ( { value } ) => ( {
		featured_media: value ?? 0,
	} ),
	enableSorting: false,
	filterBy: false,
	isValid: {
		custom: validateRequired,
	},
};

You should also update the post edit form to pass the validity object.

import { DataForm, useFormValidity } from '@wordpress/dataviews';

....

const data = ids.length === 1 ? record : multiEdits;
const { validity } = useFormValidity( data, fieldsWithDependency, form );

return (
	<VStack spacing={ 4 }>
		<PostCardPanel postType={ postType } postId={ ids } />
		{ hasFinishedResolution && (
			<DataForm
				data={ data }
				fields={ fieldsWithDependency }
				form={ form }
				onChange={ onChange }
				validity={ validity }
			/>
		) }
	</VStack>
);

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

@ntsekouras ntsekouras self-assigned this Jan 16, 2026
@ntsekouras ntsekouras requested a review from gigitux as a code owner January 16, 2026 19:18
@ntsekouras ntsekouras added [Type] Enhancement A suggestion for improvement. [Feature] DataViews Work surrounding upgrading and evolving views in the site editor and beyond labels Jan 16, 2026
@github-actions github-actions bot added [Package] DataViews /packages/dataviews [Package] Fields /packages/fields labels Jan 16, 2026
@github-actions
Copy link

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: ntsekouras <ntsekouras@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.

@ntsekouras ntsekouras force-pushed the media-edit-required-validation branch from 0e1253e to 1cf9669 Compare January 19, 2026 13:39
Comment on lines 713 to 715
( Array.isArray( value ) && !! value.length ) || value
? 'v' // We don't care about the actual value, just that it exists.
: ''
Copy link
Member

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?

Copy link
Contributor Author

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.

@ntsekouras ntsekouras force-pushed the media-edit-required-validation branch from 21309f9 to 9ff04ba Compare January 20, 2026 08:49
@github-actions
Copy link

Flaky tests detected in 9ff04ba.
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/21165134775
📝 Reported issues:

@github-actions github-actions bot removed the [Package] DataViews /packages/dataviews label Jan 20, 2026
@ntsekouras ntsekouras changed the title MediaEdit: Support required validation MediaEdit: Support custom validation Jan 20, 2026
@oandregal
Copy link
Member

This also means that in order for MediaEdit to display validation messages, the consumer should also pass the validity object in DataForm. Is this expected and/or reasonable

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.

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.

Thanks for making media-edit support validation!

@ntsekouras ntsekouras merged commit 5d035b9 into trunk Jan 20, 2026
45 checks passed
@ntsekouras ntsekouras deleted the media-edit-required-validation branch January 20, 2026 13:18
@github-actions github-actions bot added this to the Gutenberg 22.5 milestone Jan 20, 2026
adamsilverstein pushed a commit that referenced this pull request Jan 20, 2026
Co-authored-by: ntsekouras <ntsekouras@git.wordpress.org>
Co-authored-by: oandregal <oandregal@git.wordpress.org>
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] Fields /packages/fields [Type] Enhancement A suggestion for improvement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants