Automattic\WooCommerce\Blocks\Domain\Services

CheckoutFields::validate_field_for_locationpublicWC 1.0

Validates a field to check it belongs to the given location and is valid according to its registration.

This does not apply any custom validation rules on the value.

Method of the class: CheckoutFields{}

No Hooks.

Returns

true|WP_Error. True if the field is valid, a WP_Error otherwise.

Usage

$CheckoutFields = new CheckoutFields();
$CheckoutFields->validate_field_for_location( $key, $value, $location );
$key(string) (required)
The field key.
$value(mixed) (required)
The field value.
$location(string) (required)
The location to validate the field for (address|contact|order).

CheckoutFields::validate_field_for_location() code WC 9.9.3

public function validate_field_for_location( $key, $value, $location ) {
	$location = $this->prepare_location_name( $location );

	if ( ! $this->is_field( $key ) ) {
		return new WP_Error(
			'woocommerce_invalid_checkout_field',
			\sprintf(
			// translators: % is field key.
				__( 'The field %s is invalid.', 'woocommerce' ),
				$key
			)
		);
	}

	if ( ! in_array( $key, $this->fields_locations[ $location ], true ) ) {
		return new WP_Error(
			'woocommerce_invalid_checkout_field_location',
			\sprintf(
			// translators: %1$s is field key, %2$s location.
				__( 'The field %1$s is invalid for the location %2$s.', 'woocommerce' ),
				$key,
				$location
			)
		);
	}

	return true;
}