The ACF (Advanced Custom Fields) plugin provides a number field type that allows users to input numeric values. By default, ACF does not provide built-in validation for number fields. However, you can add custom validation to ensure that the entered value meets your requirements.
To add validation to an ACF number field, you can use the acf/validate_value
filter hook. This hook allows you to validate and modify the field value before it is saved to the database. Here’s an example code snippet that demonstrates how to add validation to an ACF number field:
function wpsnippets_validate_number_field($valid, $value, $field, $input_name) {
// Perform your validation logic here
if ($value < 0 || $value > 100) {
$valid = false;
$field['message'] = 'Please enter a number between 0 and 100.';
}
return $valid;
}
add_filter('acf/validate_value/type=number', 'wpsnippets_validate_number_field', 10, 4);
In the code snippet above, we define a custom validation function wpsnippets_validate_number_field
that accepts four parameters: $valid
, $value
, $field
, and $input_name
. The $valid
parameter represents the current validation status, and the $value
parameter contains the entered value. The $field
parameter holds the ACF field settings, and the $input_name
parameter is the name attribute of the input field.
Within the validation function, you can perform any custom validation logic you need. In this example, we check if the value is less than 0 or greater than 100. If the value fails the validation, we set $valid
to false
and update the field’s error message using the $field['message']
property.
Finally, we use the add_filter
function to hook our validation function to the acf/validate_value/type=number
filter. This ensures that our validation logic is applied specifically to number fields.
By implementing this code snippet, you can add custom validation to your ACF number fields and display appropriate error messages when the entered value does not meet your requirements.
Examples
Example 1: Custom validation for ACF number field
This example demonstrates how to add custom validation for an ACF number field using the acf/validate_value
filter hook.
function wpsnippets_custom_number_field_validation( $valid, $value, $field, $input_name ) {
if ( $value < 10 ) {
$valid = 'Number must be greater than or equal to 10.';
}
return $valid;
}
add_filter( 'acf/validate_value/type=number', 'wpsnippets_custom_number_field_validation', 10, 4 );
In this code example, we define a custom validation function wpsnippets_custom_number_field_validation
that checks if the value entered in the ACF number field is less than 10. If the value is less than 10, we set the $valid
variable to an error message. Otherwise, we leave it as is. Finally, we add the filter acf/validate_value/type=number
to apply the custom validation function to all ACF number fields.
Example 2: Display custom validation error message
This example demonstrates how to display a custom validation error message for an ACF number field using the acf/validate_value
and acf/render_field/type=number
filter hooks.
function wpsnippets_custom_number_field_validation( $valid, $value, $field, $input_name ) {
if ( $value < 10 ) {
$valid = 'Number must be greater than or equal to 10.';
add_filter( 'acf/render_field/type=number', 'wpsnippets_display_custom_validation_error', 10, 1 );
}
return $valid;
}
add_filter( 'acf/validate_value/type=number', 'wpsnippets_custom_number_field_validation', 10, 4 );
function wpsnippets_display_custom_validation_error( $field ) {
$field['error'] = 'Number must be greater than or equal to 10.';
return $field;
}
In this code example, we use the same custom validation function as in Example 1. However, if the value is less than 10, we add a filter acf/render_field/type=number
to modify the field and display the custom validation error message. The wpsnippets_display_custom_validation_error
function sets the error
attribute of the field to the error message.
Example 3: Prevent saving invalid value
This example demonstrates how to prevent saving an invalid value for an ACF number field using the acf/validate_value
filter hook.
function wpsnippets_custom_number_field_validation( $valid, $value, $field, $input_name ) {
if ( $value < 10 ) {
$valid = false;
}
return $valid;
}
add_filter( 'acf/validate_value/type=number', 'wpsnippets_custom_number_field_validation', 10, 4 );
In this code example, we use the same custom validation function as in Example 1. However, instead of setting an error message, we set the $valid
variable to false
if the value is less than 10. This prevents the invalid value from being saved.