A block attribute in WordPress has a type and a default, but no way to say "this must be filled in". If you build blocks in React, the usual answer is to check the values yourself and call lockPostSaving, which disables the save button in the editor. That's one more piece of code per block, and it only runs in the browser.
In Gutenberg Control Blocks (GCB) a rule is a key on the field in block.fields.json. The editor checks it as your client types, and the server refuses a save that breaks it.
Add a rule to a field
Give any field a validation object:
{ "type": "text", "attributeKey": "subtitle", "label": "Subtitle", "validation": { "required": true, "requiredMessage": "Subtitle is required.", "minLength": 3, "maxLength": 80, "pattern": "^[A-Z]", "patternMessage": "Must start with a capital letter." }}Your render.php doesn't change. It reads $attributes['subtitle'] as it always would.
The rules
| Rule | Applies to | Fails when |
|---|---|---|
required | any field | the value is empty |
minLength | text values | there are fewer characters |
maxLength | text values | there are more characters |
pattern | text values | the regular expression doesn't match |
min / max | numbers, and text that reads as a number | the value is below or above |
Each rule has a plain message, such as "Subtitle is required." or "Must be 80 characters or fewer.". Set your own with requiredMessage and patternMessage.
A few details worth knowing:
- Empty means empty for the field's type. An image with nothing picked, a link with no address, a heading with no text and a post picker with nothing chosen all count as empty.
false,0and"0"are values. - Only
requiredlooks at an empty field. The other rules apply once something is filled in, so an optional field with amaxLengthcan still be left blank. - Length counts what is stored. A
richtextorwysiwygfield stores HTML, so the tags count. Leave room for them, or limit a plaintextortextareafield instead. patternis a bare regular expression, with no delimiters. An invalid pattern is ignored rather than blocking a save.- Hidden fields are skipped. A field hidden by its
conditionalLogic, or in a hidden panel, is never checked. Your client can't fix what they can't see.
Repeaters
A repeater field is checked by its own min and max (the number of rows) and by the rules on its sub-fields, row by row. A missing title in the second row reads "Row 2, Title: Title is required."
Child blocks placed with a <Repeater> marker go through the same guard: below the marker's min or over its max, the save is refused. One block can be refused for both at once.
GCB checks a block's fields only when at least one of them has a validation key.
What your client sees
- In the sidebar, a field that breaks a rule gets a red ring with its message underneath, as soon as the value is wrong. The panel holding it opens by itself.
- On the canvas, the block shows a red banner across its top edge naming the block and the fields, such as "Hero: Heading, Intro need fixing." It sits over the block, so it doesn't push the design about.
- On save, Update or Publish is refused with one notice: "Saving failed. Hero: Heading, Intro need fixing." With several blocks, it says how many. The Find the block button selects the first one and scrolls to it.
Nothing is written while the page has errors. The saved page stays as it was, and once the fields are fixed it saves as normal.
Checked on the server, too
The editor reports each block's errors to the server as they change, through POST /gcblite/v1/validation-state, which keeps them for five minutes. A rest_pre_insert_{post_type} filter then refuses the save with a gcblite_validation_error. That covers posts, pages, and templates and template parts in the Site Editor. Autosaves are never blocked.
For a custom post type edited in the block editor, add it to the list:
add_filter( 'gcblite_validation_post_types', fn( $types ) => [ ...$types, 'case_study' ] );The same rules outside blocks
The validation key works the same way on fields you add to post types, options pages, terms and users. What happens when a rule fails depends on where the field is. In a meta box the form isn't sent until the fields pass, and if the server check fails while a post is being published, its values are saved and it goes back to draft. An options page that fails isn't stored. The validation docs cover each case.
Mistakes in the field file itself
Separate from your client's values, GCB checks each block's block.fields.json when the block is registered. With WP_DEBUG on, an invalid file gives a warning naming the exact field, such as a missing attributeKey or a duplicate id. An unknown field type is refused with the nearest real type suggested.
Try it
The rules, how a block's save is refused and the field file messages are all in the docs. The demo's hero has a required heading with a length limit: try clearing it in your browser.