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

Breadcrumb

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

class Length

Validates that a given string length is between some minimum and maximum value.

@author Bernhard Schussek <bschussek@gmail.com>

Hierarchy

  • class \Symfony\Component\Validator\Constraint
    • class \Symfony\Component\Validator\Constraints\Length extends \Symfony\Component\Validator\Constraint

Expanded class hierarchy of Length

1 file declares its use of Length
LengthConstraint.php in core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/LengthConstraint.php
4 string references to 'Length'
Connection::open in core/modules/sqlite/src/Driver/Database/sqlite/Connection.php
Opens a client connection.
CoreExtension::getFilters in vendor/twig/twig/src/Extension/CoreExtension.php
Returns a list of filters to add to the existing list.
file.schema.yml in core/modules/file/config/schema/file.schema.yml
core/modules/file/config/schema/file.schema.yml
SqlContentEntityStorageSchema::getColumnSchemaRelevantKeys in core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php
Returns a list of column schema keys affecting data storage.

File

vendor/symfony/validator/Constraints/Length.php, line 23

Namespace

Symfony\Component\Validator\Constraints
View source
class Length extends Constraint {
    public const TOO_SHORT_ERROR = '9ff3fdc4-b214-49db-8718-39c315e33d45';
    public const TOO_LONG_ERROR = 'd94b19cc-114f-4f44-9cc4-4138e80a87b9';
    public const NOT_EQUAL_LENGTH_ERROR = '4b6f5c76-22b4-409d-af16-fbe823ba9332';
    public const INVALID_CHARACTERS_ERROR = '35e6a710-aa2e-4719-b58e-24b35749b767';
    protected const ERROR_NAMES = [
        self::TOO_SHORT_ERROR => 'TOO_SHORT_ERROR',
        self::TOO_LONG_ERROR => 'TOO_LONG_ERROR',
        self::NOT_EQUAL_LENGTH_ERROR => 'NOT_EQUAL_LENGTH_ERROR',
        self::INVALID_CHARACTERS_ERROR => 'INVALID_CHARACTERS_ERROR',
    ];
    public const COUNT_BYTES = 'bytes';
    public const COUNT_CODEPOINTS = 'codepoints';
    public const COUNT_GRAPHEMES = 'graphemes';
    private const VALID_COUNT_UNITS = [
        self::COUNT_BYTES,
        self::COUNT_CODEPOINTS,
        self::COUNT_GRAPHEMES,
    ];
    public string $maxMessage = 'This value is too long. It should have {{ limit }} character or less.|This value is too long. It should have {{ limit }} characters or less.';
    public string $minMessage = 'This value is too short. It should have {{ limit }} character or more.|This value is too short. It should have {{ limit }} characters or more.';
    public string $exactMessage = 'This value should have exactly {{ limit }} character.|This value should have exactly {{ limit }} characters.';
    public string $charsetMessage = 'This value does not match the expected {{ charset }} charset.';
    public ?int $max = null;
    public ?int $min = null;
    public string $charset = 'UTF-8';
    
    /** @var callable|null */
    public $normalizer;
    
    /** @var self::COUNT_* */
    public string $countUnit = self::COUNT_CODEPOINTS;
    
    /**
     * @param positive-int|array<string,mixed>|null $exactly    The exact expected length
     * @param int<0, max>|null                      $min        The minimum expected length
     * @param positive-int|null                     $max        The maximum expected length
     * @param string|null                           $charset    The charset to be used when computing value's length (defaults to UTF-8)
     * @param callable|null                         $normalizer A callable to normalize value before it is validated
     * @param self::COUNT_*|null                    $countUnit  The character count unit for the length check (defaults to {@see Length::COUNT_CODEPOINTS})
     * @param string[]|null                         $groups
     * @param array<string,mixed>                   $options
     */
    public function __construct(int|array|null $exactly = null, ?int $min = null, ?int $max = null, ?string $charset = null, ?callable $normalizer = null, ?string $countUnit = null, ?string $exactMessage = null, ?string $minMessage = null, ?string $maxMessage = null, ?string $charsetMessage = null, ?array $groups = null, mixed $payload = null, array $options = []) {
        if (\is_array($exactly)) {
            $options = array_merge($exactly, $options);
            $exactly = $options['value'] ?? null;
        }
        $min ??= $options['min'] ?? null;
        $max ??= $options['max'] ?? null;
        unset($options['value'], $options['min'], $options['max']);
        if (null !== $exactly && null === $min && null === $max) {
            $min = $max = $exactly;
        }
        parent::__construct($options, $groups, $payload);
        $this->min = $min;
        $this->max = $max;
        $this->charset = $charset ?? $this->charset;
        $this->normalizer = $normalizer ?? $this->normalizer;
        $this->countUnit = $countUnit ?? $this->countUnit;
        $this->exactMessage = $exactMessage ?? $this->exactMessage;
        $this->minMessage = $minMessage ?? $this->minMessage;
        $this->maxMessage = $maxMessage ?? $this->maxMessage;
        $this->charsetMessage = $charsetMessage ?? $this->charsetMessage;
        if (null === $this->min && null === $this->max) {
            throw new MissingOptionsException(\sprintf('Either option "min" or "max" must be given for constraint "%s".', __CLASS__), [
                'min',
                'max',
            ]);
        }
        if (null !== $this->normalizer && !\is_callable($this->normalizer)) {
            throw new InvalidArgumentException(\sprintf('The "normalizer" option must be a valid callable ("%s" given).', get_debug_type($this->normalizer)));
        }
        if (!\in_array($this->countUnit, self::VALID_COUNT_UNITS, true)) {
            throw new InvalidArgumentException(\sprintf('The "countUnit" option must be one of the "%s"::COUNT_* constants ("%s" given).', __CLASS__, $this->countUnit));
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
Constraint::$groups public property The groups that the constraint belongs to.
Constraint::$payload public property Domain-specific data attached to a constraint.
Constraint::addImplicitGroupName public function Adds the given group if this constraint is in the Default group. 2
Constraint::CLASS_CONSTRAINT public constant Marks a constraint that can be put onto classes.
Constraint::DEFAULT_GROUP public constant The name of the group given to all constraints with no explicit group.
Constraint::getDefaultOption public function Returns the name of the default option. 28
Constraint::getErrorName public static function Returns the name of the given error code.
Constraint::getRequiredOptions public function Returns the name of the required options. 22
Constraint::getTargets public function Returns whether the constraint can be put onto classes, properties or
both.
8
Constraint::normalizeOptions protected function
Constraint::PROPERTY_CONSTRAINT public constant Marks a constraint that can be put onto properties.
Constraint::validatedBy public function Returns the name of the class that validates this constraint. 9
Constraint::__get public function Returns the value of a lazily initialized option. 2
Constraint::__isset public function 1
Constraint::__set public function Sets the value of a lazily initialized option. 1
Constraint::__sleep public function Optimizes the serialized value to minimize storage space.
Length::$charset public property
Length::$charsetMessage public property
Length::$countUnit public property @var self::COUNT_*
Length::$exactMessage public property
Length::$max public property
Length::$maxMessage public property
Length::$min public property
Length::$minMessage public property
Length::$normalizer public property @var callable|null
Length::COUNT_BYTES public constant
Length::COUNT_CODEPOINTS public constant
Length::COUNT_GRAPHEMES public constant
Length::ERROR_NAMES protected constant Maps error codes to the names of their constants. Overrides Constraint::ERROR_NAMES
Length::INVALID_CHARACTERS_ERROR public constant
Length::NOT_EQUAL_LENGTH_ERROR public constant
Length::TOO_LONG_ERROR public constant
Length::TOO_SHORT_ERROR public constant
Length::VALID_COUNT_UNITS private constant
Length::__construct public function Overrides Constraint::__construct 1
RSS feed
Powered by Drupal