Automattic\WooCommerce\Blocks\Domain\Services\CheckoutFieldTypes
DateFieldType::validate
Validates that a submitted value is a real calendar date within the field's min/max constraints.
Метод класса: DateFieldType{}
Хуков нет.
Возвращает
WP_Error|null. Error if the value is not valid, null otherwise.
Использование
$DateFieldType = new DateFieldType(); $DateFieldType->validate( $value, $field );
- $value(разное) (обязательный)
- The submitted value.
- $field(массив) (обязательный)
- The field.
Код DateFieldType::validate() DateFieldType::validate WC 11.2.2
public function validate( $value, array $field ) {
// An empty value is not a type error. Required fields are handled by the field's validation callback.
if ( null === $value || '' === $value ) {
return null;
}
if ( ! is_string( $value ) || ! TimeUtil::is_valid_date( $value, 'Y-m-d' ) ) {
return new WP_Error(
'woocommerce_invalid_checkout_field',
sprintf(
/* translators: %s: is the field label */
__( 'Please provide a valid %s in YYYY-MM-DD format.', 'woocommerce' ),
$field['label']
)
);
}
[ 'min' => $min, 'max' => $max ] = $this->get_constraints( $field, true );
// Both sides are YYYY-MM-DD, so a string comparison orders them correctly.
if ( $min && $value < $min ) {
return new WP_Error(
'woocommerce_invalid_checkout_field',
sprintf(
/* translators: 1: is the field label, 2: is the earliest date allowed */
__( 'Please provide a %1$s on or after %2$s.', 'woocommerce' ),
$field['label'],
$this->format_value( $min, $field )
)
);
}
if ( $max && $value > $max ) {
return new WP_Error(
'woocommerce_invalid_checkout_field',
sprintf(
/* translators: 1: is the field label, 2: is the latest date allowed */
__( 'Please provide a %1$s on or before %2$s.', 'woocommerce' ),
$field['label'],
$this->format_value( $max, $field )
)
);
}
return null;
}