type(); } /** * Checks the date. * * @param Field $field Field being validated. * @param mixed $value Sanitised value. * * @return ValidationError|null The failure, or null when the value passes. */ public function validate( Field $field, mixed $value ): ?ValidationError { $date = (string) $value; /* * Exactly YYYY-MM-DD, which is what a date input sends. Splitting on "-" and * casting each piece to int accepted `2024-1-1` and `2025-01-01 junk…`, and * the min/max checks below compare strings, which only works when every date * has the same zero-padded shape - so `2024-1-1` slipped under a minimum of * 2024-06-01. */ $valid = 1 === preg_match( '/^(\d{4})-(\d{2})-(\d{2})$/', $date, $parts ) && wp_checkdate( (int) $parts[2], (int) $parts[3], (int) $parts[1], $date ); if ( ! $valid ) { return new ValidationError( '', 'invalid_date', __( 'Please enter a valid date.', 'devplugins-custom-form-builder' ) ); } $min = (string) $field->setting( 'min_date', '' ); $max = (string) $field->setting( 'max_date', '' ); if ( '' !== $min && $date < $min ) { return new ValidationError( '', 'date_too_early', sprintf( /* translators: %s: Earliest accepted date. */ __( 'Please choose a date on or after %s.', 'devplugins-custom-form-builder' ), $min ), array( 'min' => $min ) ); } if ( '' !== $max && $date > $max ) { return new ValidationError( '', 'date_too_late', sprintf( /* translators: %s: Latest accepted date. */ __( 'Please choose a date on or before %s.', 'devplugins-custom-form-builder' ), $max ), array( 'max' => $max ) ); } return null; } }