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 );
},
) );
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
📣 “ACF Chat Fridays”
— Advanced Custom Fields (@wp_acf) January 31, 2023
The ACF team holds their first open office hours this Friday! Come and talk ACF, and ask questions about building sites with the plugin.
We’d love to see you there!
📆 Friday 3rd Feb - 3pm UTC
👉 Register here - https://t.co/3UtvQbE4CU pic.twitter.com/oTwW9K1XQ0
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.