acf_field_file::validate_rest_value
Validates file fields updated via the REST API.
Method of the class: acf_field_file{}
No Hooks.
Returns
true|false|WP_Error.
Usage
$acf_field_file = new acf_field_file(); $acf_field_file->validate_rest_value( $valid, $value, $field );
- $valid(true|false) (required)
- The current validity booleean.
- $value(int) (required)
- The value of the field.
- $field(array) (required)
- The field array.
acf_field_file::validate_rest_value() acf field file::validate rest value code ACF 6.8.8
public function validate_rest_value( $valid, $value, $field ) {
if ( is_null( $value ) && empty( $field['required'] ) ) {
return $valid;
}
/**
* A bit of a hack, but we use `wp_prepare_attachment_for_js()` here
* since it returns all the data we need to validate the file, and we use this anyways
* to validate fields updated via UI.
*/
$attachment = wp_prepare_attachment_for_js( $value );
$param = sprintf( '%s[%s]', $field['prefix'], $field['name'] );
$data = array(
'param' => $param,
'value' => (int) $value,
);
if ( ! $attachment ) {
$error = sprintf( __( '%s requires a valid attachment ID.', 'acf' ), $param );
return new WP_Error( 'rest_invalid_param', $error, $data );
}
$errors = acf_validate_attachment( $attachment, $field, 'prepare' );
if ( ! empty( $errors ) ) {
$error = $param . ' - ' . implode( ' ', $errors );
return new WP_Error( 'rest_invalid_param', $error, $data );
}
return $valid;
}