The validate_field
action in Advanced Custom Fields (ACF) allows you to perform custom validation on a field before it is saved. This action is triggered when the ACF form is submitted and can be useful in scenarios where you need to validate user input or perform additional checks before saving the field value.
To use the validate_field
action, you need to add a callback function that will be executed when the action is triggered. Inside this function, you can access the field value and perform your validation logic. If the validation fails, you can add an error message to the field using the acf_add_validation_error()
function.
Here’s an example of how to use the validate_field
action to validate a text field:
function wpsnippets_validate_text_field($valid, $value, $field, $input) {
// Perform your validation logic here
if (empty($value)) {
$valid = false;
acf_add_validation_error($field['key'], 'Please enter a value for this field.');
}
return $valid;
}
add_filter('acf/validate_field/name=your_text_field', 'wpsnippets_validate_text_field', 10, 4);
In this example, we define a callback function wpsnippets_validate_text_field
that takes four parameters: $valid
, $value
, $field
, and $input
. The $valid
parameter represents the current validation status, $value
contains the submitted field value, $field
holds the field settings, and $input
contains the submitted form data.
Inside the callback function, we check if the $value
is empty. If it is, we set $valid
to false
and add a validation error message using acf_add_validation_error()
. Finally, we return the updated $valid
value.
To apply this validation to a specific text field, we use the acf/validate_field/name=your_text_field
filter, where your_text_field
is the name of the text field you want to validate. You can find the field name by inspecting the field settings in the ACF admin interface.
By using the validate_field
action and adding custom validation logic, you can ensure that the submitted field values meet your requirements before they are saved. This can help maintain data integrity and improve the user experience by providing meaningful error messages.
Examples
Example 1: Custom validation for a text field in ACF
This example demonstrates how to use the validate_field
action in ACF to add custom validation for a text field. The code checks if the entered value is a valid email address and displays an error message if it’s not.
function wpsnippets_validate_email_field($valid, $value, $field, $input_name) {
if ($field['type'] === 'text' && $field['name'] === 'email') {
if (!filter_var($value, FILTER_VALIDATE_EMAIL)) {
$valid = 'Please enter a valid email address.';
}
}
return $valid;
}
add_filter('acf/validate_field', 'wpsnippets_validate_email_field', 10, 4);
In this code, we define a custom function wpsnippets_validate_email_field
that accepts four parameters: $valid
(the current validation status), $value
(the entered value), $field
(the field object), and $input_name
(the input name attribute). We check if the field is a text field with the name “email” and use filter_var
with FILTER_VALIDATE_EMAIL
to validate the email address. If it’s not valid, we update the $valid
variable with an error message. Finally, we return the updated $valid
value. The add_filter
function hooks our custom validation function to the acf/validate_field
action.
Example 2: Custom validation for a number field in ACF
This example demonstrates how to use the validate_field
action in ACF to add custom validation for a number field. The code checks if the entered value is within a specified range and displays an error message if it’s not.
function wpsnippets_validate_number_field($valid, $value, $field, $input_name) {
if ($field['type'] === 'number' && $field['name'] === 'quantity') {
$min = 1;
$max = 100;
if ($value < $min || $value > $max) {
$valid = 'Please enter a number between ' . $min . ' and ' . $max . '.';
}
}
return $valid;
}
add_filter('acf/validate_field', 'wpsnippets_validate_number_field', 10, 4);
In this code, we define a custom function wpsnippets_validate_number_field
that accepts the same four parameters as in the previous example. We check if the field is a number field with the name “quantity” and define the minimum and maximum allowed values. If the entered value is outside this range, we update the $valid
variable with an error message. Finally, we return the updated $valid
value. The add_filter
function hooks our custom validation function to the acf/validate_field
action.
Example 3: Custom validation for a select field in ACF
This example demonstrates how to use the validate_field
action in ACF to add custom validation for a select field. The code checks if a specific option is selected and displays an error message if it’s not.
function wpsnippets_validate_select_field($valid, $value, $field, $input_name) {
if ($field['type'] === 'select' && $field['name'] === 'color') {
$required_option = 'red';
if ($value !== $required_option) {
$valid = 'Please select the option "' . $required_option . '".';
}
}
return $valid;
}
add_filter('acf/validate_field', 'wpsnippets_validate_select_field', 10, 4);
In this code, we define a custom function wpsnippets_validate_select_field
that accepts the same four parameters as in the previous examples. We check if the field is a select field with the name “color” and define the required option as “red”. If the selected option is not “red”, we update the $valid
variable with an error message. Finally, we return the updated $valid
value. The add_filter
function hooks our custom validation function to the acf/validate_field
action.