gutenberg icon indicating copy to clipboard operation
gutenberg copied to clipboard

Block validation failing for Array attribute

Open slpy9 opened this issue 4 years ago • 2 comments

Description

When I set the value of an array attribute in the edit function and save/reload, block validation fails.

The post updates correctly, but the content generated by the save function is displaying the default values. It's like the save function is failing to parse the attribute from post content.

I've provided a full code example below.

Step-by-step reproduction instructions

  1. Create a new block
  2. Use the code below as a test case

Screenshots, screen recording, code snippet

import { registerBlockType } from '@wordpress/blocks';
import { useBlockProps } from '@wordpress/block-editor';

const attributes = {
	test: {
		type: 'array',
		default: [ 'one', 'two', 'three' ],
		selector: 'li',
		source: 'text',
	}
};

const List = ( { items } ) => (
	<ul>
		{ items.map( ( value, index ) => (
			<li key={ index }>{ value }</li>
		) ) }
	</ul>
);

registerBlockType( 'dev/test-block', { title: 'Test Block', category: 'text', attributes,

	edit: ( { attributes, setAttributes } ) => {
		const { test } = attributes;
		const update = () => setAttributes( { test: [ 'four', 'five', 'six' ] } );

		return (
			<div { ...useBlockProps() }>
				<button onClick={ update }>Update</button>
				<List items={ test } />
			</div>
		);
	},

	save: ( { attributes } ) => {
		const { test } = attributes;

		return (
			<div { ...useBlockProps.save() }>
				<List items={ test } />
			</div>
		);
	},
} );

Environment info

  • WordPress 6.0.2, Gutenberg 14.1.0, Twenty Twenty-Two
  • Chrome / Firefox
  • Desktop with Windows 10

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

Yes

slpy9 avatar Sep 19 '22 17:09 slpy9

@slpy9 The issue is the way the attribute is configured to source from HTML text:

selector: 'li',
source: 'text',

This will always just select the first <li> element and get the text, which is a string type, so will cause parsing to fail.

There is a solution though, what you're trying to achieve is possible using a source: 'query': https://developer.wordpress.org/block-editor/reference-guides/block-api/block-attributes/#query-source

This will allow the selector to match each <li> element. The trade-off is that your array won't be a an array of values ([ 'one', 'two', ... ]), but an array of objects ([ { value: 'one' }, { value: 'two' }, ... ]), but this at least opens up the possibility of sourcing more values from the list items as additional properties in the objects.

talldan avatar Sep 20 '22 02:09 talldan

@talldan thanks for clarifying! that makes so much sense now that you point it out.. I wasn't sure if I was doing something wrong or not, as the content was changing and updating correctly, but also causing the validation error.

So an array attribute should only be used with a query/comment source? perhaps this could be made clearer in the documentation?

slpy9 avatar Sep 20 '22 03:09 slpy9