WooCommerce Code Reference

TextFieldType extends AbstractFieldType
in package

The "text" additional checkout field type.

Text fields have no type-specific options or constraints, so the AbstractFieldType defaults apply.

Table of Contents

default_validate()  : WP_Error|void
The validate_callback a field gets when it does not declare its own: rejects empty required fields.
format_value()  : mixed
Formats a stored value for display based on the field type.
from_storage()  : mixed
Converts a stored meta value into the type the rest of checkout works with.
prepare_form_field()  : array<string|int, mixed>
Applies type-specific arguments to a field before it is rendered with woocommerce_form_field().
prepare_validation_schema()  : array<string|int, mixed>
Adds type-specific defaults to a field's validation schema.
prepare_value_schema()  : array<string|int, mixed>
Applies type-specific keywords to a field's REST API value schema.
process_options()  : array<string|int, mixed>|false
Processes and validates the options supplied during field registration.
sanitize()  : mixed
Sanitizes a submitted value for this field type.
validate()  : WP_Error|null
Validates a submitted value against the constraints of this field type.
validate_options()  : bool
Validates the options that apply to every field type: callbacks, hidden state, and rule schemas.
doing_it_wrong()  : false
Reports a field registration misuse that does not prevent the field from being registered.
process_attributes()  : array<string|int, mixed>
Processes the attributes supplied during field registration.
process_type_options()  : array<string|int, mixed>|false
Processes the type-specific options supplied during field registration.
registration_error()  : false
Reports a field registration error and prevents the field from being registered.

Methods

default_validate()

The validate_callback a field gets when it does not declare its own: rejects empty required fields.

public default_validate(mixed $value, array<string|int, mixed> $field) : WP_Error|void

Unlike validate(), a field's validate_callback can be replaced at registration, so nothing here is mandatory for the type.

Parameters
$value : mixed

The submitted value.

$field : array<string|int, mixed>

The field.

Return values
WP_Error|void — An error if the value is invalid.

format_value()

Formats a stored value for display based on the field type.

public format_value(mixed $value, array<string|int, mixed> $field) : mixed
Parameters
$value : mixed

The stored value.

$field : array<string|int, mixed>

The field.

Return values
mixed — The formatted value.

prepare_form_field()

Applies type-specific arguments to a field before it is rendered with woocommerce_form_field().

public prepare_form_field(array<string|int, mixed> $form_field) : array<string|int, mixed>
Parameters
$form_field : array<string|int, mixed>

The woocommerce_form_field() arguments built from the field.

Return values
array<string|int, mixed> — The updated arguments.

prepare_validation_schema()

Adds type-specific defaults to a field's validation schema.

public prepare_validation_schema(array<string|int, mixed> $schema) : array<string|int, mixed>
Parameters
$schema : array<string|int, mixed>

The supplied validation schema.

Tags
since
11.2.0
Return values
array<string|int, mixed> — The schema with defaults applied.

prepare_value_schema()

Applies type-specific keywords to a field's REST API value schema.

public prepare_value_schema(array<string|int, mixed> $field_schema, array<string|int, mixed> $field) : array<string|int, mixed>
Parameters
$field_schema : array<string|int, mixed>

The schema built for the field so far.

$field : array<string|int, mixed>

The field.

Return values
array<string|int, mixed> — The updated schema.

process_options()

Processes and validates the options supplied during field registration.

public final process_options(array<string|int, mixed> $field_data, array<string|int, mixed> $options) : array<string|int, mixed>|false
Parameters
$field_data : array<string|int, mixed>

The field data array to be updated.

$options : array<string|int, mixed>

The options supplied during field registration.

Return values
array<string|int, mixed>|false — The updated $field_data array, or false if an error should prevent registration.

sanitize()

Sanitizes a submitted value for this field type.

public sanitize(mixed $value, array<string|int, mixed> $field) : mixed
Parameters
$value : mixed

The submitted value.

$field : array<string|int, mixed>

The field.

Return values
mixed — The sanitized value.

validate()

Validates a submitted value against the constraints of this field type.

public validate(mixed $value, array<string|int, mixed> $field) : WP_Error|null
Parameters
$value : mixed

The submitted value.

$field : array<string|int, mixed>

The field.

Return values
WP_Error|null — Error if the value is not valid for the field type, null otherwise.

validate_options()

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

public validate_options(array<string|int, mixed> $options) : bool

Subclasses adding checks should call this parent method first.

Parameters
$options : array<string|int, mixed>

The options supplied during field registration.

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

doing_it_wrong()

Reports a field registration misuse that does not prevent the field from being registered.

protected doing_it_wrong(string $message, string $version) : false
Parameters
$message : string

The message describing the misuse.

$version : string

The version the misuse was introduced in.

Return values
false —

process_attributes()

Processes the attributes supplied during field registration.

protected process_attributes(string $id, mixed $attributes) : array<string|int, mixed>

Invalid attributes are dropped with a warning rather than preventing registration.

Parameters
$id : string

The field ID.

$attributes : mixed

The attributes supplied during field registration.

Return values
array<string|int, mixed> — The processed attributes.

process_type_options()

Processes the type-specific options supplied during field registration.

protected process_type_options(array<string|int, mixed> $field_data, array<string|int, mixed> $options) : array<string|int, mixed>|false
Parameters
$field_data : array<string|int, mixed>

The field data array, with the options common to every type already applied.

$options : array<string|int, mixed>

The options supplied during field registration.

Return values
array<string|int, mixed>|false — The updated $field_data array, or false if an error should prevent registration.

registration_error()

Reports a field registration error and prevents the field from being registered.

protected registration_error(string $id, string $reason, string $version) : false
Parameters
$id : string

The ID of the field being registered.

$reason : string

The reason the field cannot be registered.

$version : string

The version the misuse was introduced in.

Return values
false —