acf_validate_value()
acf_validate_value
This function will validate a field's value
Hooks from the function
Returns
null. Nothing (null).
Usage
acf_validate_value( $value, $field, $input );
- $value(required)
- .
- $field(required)
- .
- $input(required)
- .
Changelog
| Since 5.0.0 | Introduced. |
acf_validate_value() acf validate value code ACF 6.0.4
function acf_validate_value( $value, $field, $input ) {
// vars
$valid = true;
$message = sprintf( __( '%s value is required', 'acf' ), $field['label'] );
// valid
if ( $field['required'] ) {
// valid is set to false if the value is empty, but allow 0 as a valid value
if ( empty( $value ) && ! is_numeric( $value ) ) {
$valid = false;
}
}
/**
* Filters whether the value is valid.
*
* @date 28/09/13
* @since 5.0.0
*
* @param bool $valid The valid status. Return a string to display a custom error message.
* @param mixed $value The value.
* @param array $field The field array.
* @param string $input The input element's name attribute.
*/
$valid = apply_filters( "acf/validate_value/type={$field['type']}", $valid, $value, $field, $input );
$valid = apply_filters( "acf/validate_value/name={$field['_name']}", $valid, $value, $field, $input );
$valid = apply_filters( "acf/validate_value/key={$field['key']}", $valid, $value, $field, $input );
$valid = apply_filters( 'acf/validate_value', $valid, $value, $field, $input );
// allow $valid to be a custom error message
if ( ! empty( $valid ) && is_string( $valid ) ) {
$message = $valid;
$valid = false;
}
if ( ! $valid ) {
acf_add_validation_error( $input, $message );
return false;
}
// return
return true;
}