acf_add_validation_error()
Adds a validation error and prevents submitted ACF form data from being saved.
It is usually used on the acf/validate_save_post hook.
No Hooks.
Returns
n/a. Nothing. Stops the data-saving process.
Usage
acf_add_validation_error( $input, $message );
- $input (required)
The value of the
nameattribute of the field for which to display an error. The format isacf[field key], for exampleacf[field_615baebe3f16d]. JavaScript uses this value to find the field.

If an empty string is passed, the error applies to the general error and is displayed above the submitted form.

- $message
- The error message, for example
Chief, everything is lost! Everything is lost!.
Default:''
Examples
#1 Check a field and display an error
add_action('acf/validate_save_post', 'my_acf_validate_save_post');
function my_acf_validate_save_post() {
// Remove all errors for the administrator (remove this code if not needed).
if( current_user_can('manage_options') ) {
acf_reset_validation_errors();
}
// Make the field required.
if( empty($_POST['acf']['field_615baebe3f16d']) ) {
acf_add_validation_error( 'acf[field_615baebe3f16d]', 'Fill in this field!' );
}
}
#2 Returned data
Example returned data for a single field:
{
"success": true,
"data": {
"valid": 0,
"errors": [
{
"input": "acf[field_615baebe3f16d]",
"message": "This price is too low for this type of work."
}
]
}
}
Example returned data for a general error:
{
"success": true,
"data": {
"valid": 0,
"errors": [
{
"input": "",
"message": "You must specify a shipping price for at least one vehicle type: \"Passenger car\", \"SUV\", or \"Minibus\"."
}
]
}
}Changelog
| Since 5.0.0 | Introduced. |
acf_add_validation_error() acf add validation error code ACF 6.8.8
function acf_add_validation_error( $input, $message = '' ) {
return acf()->validation->add_error( $input, $message );
}