Automattic\WooCommerce\Blocks\Domain\Services\CheckoutFieldTypes

AbstractFieldType::validate_options │ public │ WC 1.0

Validates the options that apply to every field type: callbacks, hidden state, and rule schemas.

Subclasses adding checks should call this parent method first.

Method of the class: AbstractFieldType{}

No Hooks.

Returns

bool. False if an error should prevent registration, true otherwise.

Usage

$AbstractFieldType = new AbstractFieldType();
$AbstractFieldType->validate_options( $options ): bool;
$options(array) (required)
The options supplied during field registration.

AbstractFieldType::validate_options() code WC 11.2.1

public function validate_options( array $options ): bool {
	$id = $options['id'];

	if ( empty( $options['label'] ) ) {
		return $this->registration_error( $id, 'The field label is required.', '8.6.0' );
	}

	foreach ( array( 'sanitize_callback', 'validate_callback' ) as $callback ) {
		if ( ! empty( $options[ $callback ] ) && ! is_callable( $options[ $callback ] ) ) {
			return $this->registration_error( $id, sprintf( 'The %s must be a valid callback.', $callback ), '8.6.0' );
		}
	}

	if ( ! empty( $options['hidden'] ) && true === $options['hidden'] ) {
		// Not an error: the field is still registered, just as a visible one.
		$this->doing_it_wrong( sprintf( 'Registering a field with hidden set to true is not supported. The field "%s" will be registered as visible.', $id ), '8.6.0' );
	}

	foreach ( array( 'required', 'hidden', 'validation' ) as $rule_field ) {
		if ( empty( $options[ $rule_field ] ) || ( 'validation' !== $rule_field && is_bool( $options[ $rule_field ] ) ) ) {
			continue;
		}

		$valid = Validation::is_valid_schema( $options[ $rule_field ] );

		if ( is_wp_error( $valid ) ) {
			return $this->registration_error( $id, $rule_field . ': ' . $valid->get_error_message(), '8.6.0' );
		}
	}

	return true;
}