Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Bytes.php

function Bytes::validateConstraint

Validates a string is a representation of a number of bytes.

To be used with the `Callback` constraint.

Parameters

string|int|float|null $value: The string, integer or float to validate.

\Symfony\Component\Validator\Context\ExecutionContextInterface $context: The validation execution context.

See also

\Symfony\Component\Validator\Constraints\CallbackValidator

core/config/schema/core.data_types.schema.yml

File

core/lib/Drupal/Component/Utility/Bytes.php, line 127

Class

Bytes
Provides helper methods for byte conversions.

Namespace

Drupal\Component\Utility

Code

public static function validateConstraint(string|int|float|null $value, ExecutionContextInterface $context) : void {
    // Ignore NULL values (i.e. support `nullable: true`).
    if ($value === NULL) {
        return;
    }
    if (!self::validate((string) $value)) {
        $context->addViolation('This value must be a number of bytes, optionally with a unit such as "MB" or "megabytes". %value does not represent a number of bytes.', [
            '%value' => $value,
        ]);
    }
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal