function Bytes::validateConstraint

Same name in other branches
  1. 11.x core/lib/Drupal/Component/Utility/Bytes.php \Drupal\Component\Utility\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

1 call to Bytes::validateConstraint()
BytesTest::testValidate in core/tests/Drupal/Tests/Component/Utility/BytesTest.php
Tests \Drupal\Component\Utility\Bytes::validate().

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,
        ]);
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.