GCBGutenberg
Control Blocks
Buy

Building blocks

Validation

Per-field validation (user input)

Every control supports a validation block that the meta-box and (in future) the block Inspector enforce client-side, with a server-side mirror that forces the post to draft on save if invalid:

JSON
{
  "type": "text",
  "attributeKey": "subtitle",
  "label": "Subtitle",
  "validation": {
    "required": true,
    "requiredMessage": "Subtitle is required.",
    "minLength": 3,
    "maxLength": 80,
    "min": 0,
    "max": 100,
    "pattern": "^[A-Z]",
    "patternMessage": "Must start with a capital letter."
  }
}
  • required can be a boolean OR { "message": "..." }.
  • requiredMessage is a top-level shorthand for a custom required text.
  • minLength / maxLength apply to string values.
  • min / max apply to numbers (and numeric strings).
  • pattern is a bare regex (no delimiters); supply patternMessage to override the generic "does not match" error.

Hidden by conditional logic = skipped. If a required field is hidden by its conditionalLogic, it is excluded from validation entirely — it can't block save because the user can't see it.

Server-side authority. GCBLite\PostFields\Validator mirrors the JS rules. If a post is saved with invalid required fields, status is forced to draft and an admin notice lists each error. Add new rules to BOTH src/validation.js and includes/PostFields/Validator.php — the JS-only client check is just nice UX.

Config validation (registration-time)

The plugin validates gcb configs at registration. With WP_DEBUG on, invalid blocks emit warnings naming the exact field. The scaffold CLI rejects invalid specs before writing anything.

Before reporting a block as done:

  1. block.json parses and has apiVersion: 3, name: "gcb/{slug}".
  2. Every non-group control in block.fields.json has attributeKey. Every parentPanelId matches a group's id.
  3. If using render.php: php -l render.php passes, and every attribute referenced has a matching control in block.fields.json.
  4. If rendering via your Next.js frontend: a route at /wordpress/render/{slug} on that frontend returns the component wrapped in <wp-block-wrapper data-block-name="{slug}">.

Validator messages

Reference, generated from the plugin source

The messages GCBLite\Validation\BlockGcbValidator gives when a block.fields.json is wrong, read from the code. Each names the path of the control it is about (for example controls[2].attributeKey).

  • block.fields.json must be an object.
  • controls must be an array.
  • allowed_blocks must be null or an array of block names.
  • Each entry must be a string.
  • Control must be an object.
  • {required} is required and must be a non-empty string.
  • Duplicate control id {id}.
  • attributeKey is required for non-group controls.
  • attributeKey must start with a letter and contain only letters, digits, and underscores.
  • attributeType must be one of:
  • parentPanelId references unknown control {control}. Must match a structural control's id (group / panel / tools-panel).
  • decimals belongs to a number or range control: it is how many decimals the number prints with.
  • decimals must be a whole number of zero or more: the count of decimals the field prints with ("5.0" is 1, "4.95" is 2, "14,395" is 0).
  • options must be an array.
  • Each option must be {label, value}.

Unknown types are refused, with the nearest real type suggested: "Unknown control type x. Did you mean y? Use a built-in type or register your own with gcblite_register_control_type()."

Built from GCB Lite 0.4.0 (ce232d4). Source: AGENTS.md, includes/Validation/BlockGcbValidator.php.

Copyright © 2026 Runchain. WordPress is a trademark of the WordPress Foundation.