respect-validation/docs/rules/Length.md
Danilo Correa 17f8c50f54
Apply contribution guidelines to "Length" rule
Because of the type hinting some validation could be removed from the
"length" constructor.

While applying the contribution guidelines we could also see some
duplicated logic in the "extractLength" method and that the rule was
validating

Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2018-09-06 08:41:11 +02:00

1,003 B

Length

  • Length(int $min, int $max)
  • Length(int $min, null)
  • Length(null, int $max)
  • Length(int $min, int $max, bool $inclusive)

Validates the length of the given input.

Most simple example:

v::stringType()->length(1, 5)->validate('abc'); // true

You can also validate only minimum length:

v::stringType()->length(5, null)->validate('abcdef'); // true

Only maximum length:

v::stringType()->length(null, 5)->validate('abc'); // true

The type as the first validator in a chain is a good practice, since length accepts many types:

v::arrayVal()->length(1, 5)->validate(['foo', 'bar']); // true

A third parameter may be passed to validate the passed values inclusive:

v::stringType()->length(1, 5, true)->validate('a'); // true

Message template for this validator includes {{minValue}} and {{maxValue}}.

Changelog

Version Description
0.3.9 Created

See also: