Support

Account

Home Forums Gutenberg Required fields in Gutenberg editor Reply To: Required fields in Gutenberg editor

  • @bitlife Yes, you can make a field required when using the Gutenberg editor. To do this, you can use the isRequired property of the field configuration object.

    For example, if you are using the TextControl component to create a text field, you can set the isRequired property to true like this:

    import { TextControl } from '@wordpress/components';
    
    function MyTextControl( { label, onChange } ) {
      return (
        <TextControl
          label={ label }
          onChange={ onChange }
          isRequired
        />
      );
    }

    This will make the text field required and display an error message if the user tries to submit the form without entering a value.

    If you are using Advanced Custom Fields (ACF) to create your fields, you can also make a field required by setting the required parameter to true when you register the field. For example:

    acf_add_local_field(array(
      'key' => 'field_123',
      'label' => 'My Field',
      'name' => 'my_field',
      'type' => 'text',
      'required' => true,
    ));

    As for saving the data to wp_postmeta instead of the blocks HTML comment, you can use the save callback function when registering your block. This function allows you to specify how the block data should be saved when the post is updated.

    Here is an example of how you could use the save callback function to save the block data to wp_postmeta:

    register_block_type( 'my-plugin/my-block', array(
      'save' => function( $attributes ) {
        update_post_meta( $attributes['postId'], 'my_block_data', $attributes );
      },
    ) );