acf_validate_block_from_local_meta()ACF 6.3.1

Handle the specific validation for a block from local meta.

This function uses the values loaded into Local Meta, which means they have to be converted back to the data format because they can be validated.

No Hooks.

Returns

Array|true|false. An array containing the validation errors, or false if there are no errors.

Usage

acf_validate_block_from_local_meta( $block_id, $field_objects, $using_defaults );
$block_id(string) (required)
The block ID.
$field_objects(array) (required)
The field objects in local meta to be validated.
$using_defaults(true|false)
True if this is the first load of the block, when special validation may apply.
Default: false

Changelog

Since 6.3.1 Introduced.

acf_validate_block_from_local_meta() code ACF 6.8.8

function acf_validate_block_from_local_meta( $block_id, $field_objects, $using_defaults = false ) {
	if ( empty( $field_objects ) ) {
		return false;
	}

	$using_loaded_meta = false;
	if ( acf_get_data( $block_id . '_loaded_meta_values' ) ) {
		$using_loaded_meta = true;
	}

	acf_reset_validation_errors();
	foreach ( $field_objects as $field ) {
		// Skip for nested fields - these don't work correctly on initial load of a saved block.
		if ( ! empty( $field['sub_fields'] ) ) {
			continue;
		}

		// If we're using default values, or loaded meta we may have values which are about to be populated at field render, so shouldn't raise errors here.
		if ( $using_defaults || $using_loaded_meta ) {
			// Fields with conditional logic applied shouldn't be validated during first load as conditionals aren't respected.
			if ( ! empty( $field['conditional_logic'] ) ) {
				continue;
			}

			// If we've got a empty value with a default value set and it's first load, don't produce a validation error as it will be substituted on render.
			if ( $field['required'] && empty( $field['value'] ) && ! empty( $field['default_value'] ) ) {
				continue;
			}

			// If we're loading a few radio or select-like fields, without allow null, HTML will automatically select the first value on render, so skip here.
			if ( $field['required'] && in_array( $field['type'], array( 'radio', 'button_group', 'select' ), true ) && ! $field['allow_null'] ) {
				continue;
			}
		}

		$key   = $field['key'];
		$value = $field['value'];
		acf_validate_value( $value, $field, "acf-{$block_id}[{$key}]" );
	}

	return acf_get_validation_errors();
}