Skip to content

Validation

The framework runs two validation passes on save: automatic per-type checks (email format, URL format, numeric bounds, hex colour, date / time format) and any custom rules you declare under the validation key on a field.

These run for free, no configuration:

Field typeBuilt-in check
emailRFC-compliant email format
urlURL format
numberNumeric, respects args.min / args.max / args.integer
rangeSame as number
colorHex format (#RGB or #RRGGBB)
dateYYYY-MM-DD format
timeHH:MM (24h)
select (single)Value must be in declared options keys
radioValue must be in declared options keys
[
'id' => 'username',
'type' => 'text',
'label' => __('Username', 'my-plugin'),
'required' => true,
'validation' => 'min:3|max:20|regex:/^[a-z0-9_]+$/',
],

Rules use magewirephp/validation syntax (a maintained fork of rakit/validation) — pipe-separated, with parameters after a colon. All of its built-in rules are available, including:

RuleNotes
requiredUse the promoted required key instead
min:N / max:NString length for strings, value for numbers
between:min,maxNumeric range
emailEmail format (email field already has this)
urlURL format (url field already has this)
regex:/.../Regular expression match
alphaLetters only
alpha_numLetters and digits only
alpha_dashLetters, digits, dashes, underscores
in:a,b,cValue must be in the list
not_in:a,b,cValue must NOT be in the list
same:other_fieldMust equal another field’s value
different:other_fieldMust differ from another field’s value
before:DATEDate must be before
after:DATEDate must be after

Validation failures return 400 from the REST save endpoint with an error map:

{
"code": "validation_failed",
"message": "Validation failed.",
"data": {
"status": 400,
"errors": {
"username": "Username must be at least 3 characters.",
"email": "Please enter a valid email address."
}
}
}

The React client maps each errors[field_id] entry to the matching field and surfaces an inline error message plus a snackbar summary listing the affected fields by label.

Failing validation server-side from a custom handler

Section titled “Failing validation server-side from a custom handler”

Custom handlers (action field, table actions, etc.) can short-circuit with a WP_Error that the client will surface the same way:

add_filter('my-plugin/action/default/billing_tools/wipe', function ($unhandled, array $values, $request) {
if (empty($values['confirm_token'])) {
return new WP_Error(
'validation_failed',
__('Missing confirmation token.', 'my-plugin'),
['status' => 400]
);
}
// …
}, 10, 3);