Cybo Advanced Field EngineCyboweb Technologies · Documentation · v1.0.2

cafe_check_value()

Purpose

Checks a value without saving it. Runs exactly the checks cafe_update_field() runs: the field type's validation and the cafe_validate_field filter, and tells you what is wrong. Useful before an import, in a form handler, or to test data from another system.

Syntax

cafe_check_value( string $selector, mixed $value, mixed $object_id = null, ?string $object_type = null ): true|WP_Error

Parameters

Parameter Type Default Description
$selector string - Field name or field key.
$value mixed - The value to check, in the shape cafe_update_field() accepts.
$object_id mixed null Which object. null means the current object.
$object_type string|null null Object type, or null to infer it.

Return value

true when the value can be saved, otherwise a WP_Error whose message says what is wrong. For values inside a Collection or Field Set the error data contains a path, for example team_members.2.photo.

Example

$check = cafe_check_value( 'price', $row->price, $post_id );

if ( is_wp_error( $check ) ) {
	echo esc_html( $check->get_error_message() ); // "Price must be a number."
}

To check a value for an object that does not exist yet (for example a post an import is about to create), pass an object context with ID 0:

$check = cafe_check_value( 'price', '12a', new Cybo\AdvancedFieldEngine\Values\ObjectContext( 'post', 0 ) );

Notes

  • Nothing is written and no hook that saves runs.
  • Required settings are not enforced; use cafe()->fields()->check_value( $selector, $value, $object_id, $object_type, true ) for that.
  • Since 0.13.0.