$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Wordpress-gutenberg - Block validation: Expected text|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Wordpress-gutenberg - Block validation: Expected text

matteradmin8PV0评论

I am building a gutenberg block plugin and I got stuck on simple two-field structure. The issue is that the save function works properly but something is wrong with the edit function. When I reload page I get this error: 'This block contains unexpected or invalid content.'

And in browser console I get the following error:

Block validation: Expected text lkjhlkjhlkjh, saw poiupoiupoiu. Block validation: Block validation failed for protex-contact/g-block ( Object { name: "protex-contact/g-block", title: "Protex konakt", icon: {…}, category: "common", attributes: {…}, edit: edit(), save: save() } ).

Expected:

<div class="wp-block-protex-contact-g-block"><p class="prtx_contact_name">lkjhlkjhlkjh</p><p class="prtx_contact_address">lkjhlkjhlkjh</p></div>

Actual:

<div class="wp-block-protex-contact-g-block"><p class="prtx_contact_name">lkjhlkjhlkjh</p><p class="prtx_contact_address">poiupoiupoiu</p></div>

Here is my JS:

    const { __ } = wp.i18n;
    var el = wp.element.createElement,
        registerBlockType = wp.blocks.registerBlockType,
        RichText = wp.editor.RichText;
    registerBlockType( 'protex-contact/g-block', {
        title: __( 'Protex konakt', 'protex-contact-domain' ),
        icon: 'id',
        category: 'common',
        attributes: {
            content: {
                type: 'string',
                source: 'html',
                selector: 'p',
            },
            content2:{
                type: 'string',
                source: 'html',
                selector: 'p',
            },
        },

        edit: function( props ) {
            var content = props.attributes.content,
            content2 = props.attributes.content2;

            function onChangeContent( newContent ) {
                props.setAttributes( { content: newContent } );
            }

            function onChangeContent2 ( newContent2 ){
                props.setAttributes( { content2: newContent2 } );
            }

            return el ( 'div', { className: props.className }, 
                el(
                RichText,
                {
                    tagName: 'p',
                    className: 'prtx_contact_name',
                    onChange: onChangeContent,
                    value: content,
                }
                ),
                el(
                RichText,
                {
                    tagName: 'p',
                    className: 'prtx_contact_address',
                    onChange: onChangeContent2,
                    value: content2,
                }
                ),
            );
        },

        save: function( props ) {
            var content = props.attributes.content;
            var content2 = props.attributes.content2;
            return el ( 'div', { className: props.className },
                el( RichText.Content, {
                    tagName: 'p',
                    className: 'prtx_contact_name',
                    value: content,
                } ),
                el( RichText.Content, {
                    tagName: 'p',
                    className: 'prtx_contact_address',
                    value: content2,
                } ),
            );
        },
    } );

I am building a gutenberg block plugin and I got stuck on simple two-field structure. The issue is that the save function works properly but something is wrong with the edit function. When I reload page I get this error: 'This block contains unexpected or invalid content.'

And in browser console I get the following error:

Block validation: Expected text lkjhlkjhlkjh, saw poiupoiupoiu. Block validation: Block validation failed for protex-contact/g-block ( Object { name: "protex-contact/g-block", title: "Protex konakt", icon: {…}, category: "common", attributes: {…}, edit: edit(), save: save() } ).

Expected:

<div class="wp-block-protex-contact-g-block"><p class="prtx_contact_name">lkjhlkjhlkjh</p><p class="prtx_contact_address">lkjhlkjhlkjh</p></div>

Actual:

<div class="wp-block-protex-contact-g-block"><p class="prtx_contact_name">lkjhlkjhlkjh</p><p class="prtx_contact_address">poiupoiupoiu</p></div>

Here is my JS:

    const { __ } = wp.i18n;
    var el = wp.element.createElement,
        registerBlockType = wp.blocks.registerBlockType,
        RichText = wp.editor.RichText;
    registerBlockType( 'protex-contact/g-block', {
        title: __( 'Protex konakt', 'protex-contact-domain' ),
        icon: 'id',
        category: 'common',
        attributes: {
            content: {
                type: 'string',
                source: 'html',
                selector: 'p',
            },
            content2:{
                type: 'string',
                source: 'html',
                selector: 'p',
            },
        },

        edit: function( props ) {
            var content = props.attributes.content,
            content2 = props.attributes.content2;

            function onChangeContent( newContent ) {
                props.setAttributes( { content: newContent } );
            }

            function onChangeContent2 ( newContent2 ){
                props.setAttributes( { content2: newContent2 } );
            }

            return el ( 'div', { className: props.className }, 
                el(
                RichText,
                {
                    tagName: 'p',
                    className: 'prtx_contact_name',
                    onChange: onChangeContent,
                    value: content,
                }
                ),
                el(
                RichText,
                {
                    tagName: 'p',
                    className: 'prtx_contact_address',
                    onChange: onChangeContent2,
                    value: content2,
                }
                ),
            );
        },

        save: function( props ) {
            var content = props.attributes.content;
            var content2 = props.attributes.content2;
            return el ( 'div', { className: props.className },
                el( RichText.Content, {
                    tagName: 'p',
                    className: 'prtx_contact_name',
                    value: content,
                } ),
                el( RichText.Content, {
                    tagName: 'p',
                    className: 'prtx_contact_address',
                    value: content2,
                } ),
            );
        },
    } );
Share Improve this question asked Mar 2, 2019 at 16:26 camilluskilluscamilluskillus 231 silver badge3 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 5

The problem is with your selectors:

attributes: {
    content: {
        type: 'string',
        source: 'html',
        selector: 'p',
    },
    content2:{
        type: 'string',
        source: 'html',
        selector: 'p',
    },
},

You're only using p for both attributes.

When the block looks at the saved HTML to validate it, it checks to see if the value of the selectors matches the value of the attribute. If it doesn't then the validation fails.

So given this HTML:

<div class="wp-block-protex-contact-g-block">
    <p class="prtx_contact_name">lkjhlkjhlkjh</p>
    <p class="prtx_contact_address">poiupoiupoiu</p>
</div>

It checks the value for content by looking for p, and it finds lkjhlkjhlkjh, which is the content of the first p tag. This is also the saved value for content, so things are looking good so far.

Then it looks for the value for content2 by also looking at the first p tag, since that's the selector, and this also finds lkjhlkjhlkjh. However, the block thinks that the value for content2 is supposed to be poiupoiupoiu, so it thinks that the HTML has been saved incorrectly.

So you need to make sure that your selectors can be used to find the correct value in the HTML. You can do this by using the classes you're already using on those paragraphs:

attributes: {
    content: {
        type: 'string',
        source: 'html',
        selector: 'p.prtx_contact_name',
    },
    content2:{
        type: 'string',
        source: 'html',
        selector: 'p.prtx_contact_address',
    },
},
Post a comment

comment list (0)

  1. No comments so far