respect-validation/docs/validators/Length.md
Alexandre Gomes Gaigalas 16148e9593 Standardize and improve validation message templates
- Remove redundant "valid" prefix:
   Date, DateTime, DateTimeDiff, Domain, Email, Iban, Imei, Ip, Isbn, Json, LanguageCode, LeapDate, LeapYear, Luhn, MacAddress, NfeAccessKey, Nif, Nip, Pesel, Phone, Pis, PolishIdCard, PostalCode, Roman, Slug, Tld, Url, Uuid, Version.

 - Remove redundant "value" suffix
   ArrayVal, BoolVal, Countable, FloatVal, IntVal, IterableVal, NumericVal, ScalarVal, StringVal.

 - Standardize "consist only of" phrasing
   Alnum, Alpha, Cntrl, Consonant, Digit, Graph, Lowercase, Printable, Punct, Space, Spaced, Uppercase, Vowel, Xdigit.

 - Improve file accessibility messages
   Directory, Executable, File, Image, Readable, SymbolicLink, Writable.

 - Improve grammar and article usage
   CreditCard, Extension, Mimetype, Regex, Size.
2026-02-03 19:58:55 +00:00

103 lines
2.8 KiB
Markdown

<!--
SPDX-License-Identifier: MIT
SPDX-FileCopyrightText: (c) Respect Project Contributors
SPDX-FileContributor: Alexandre Gomes Gaigalas <alganet@gmail.com>
SPDX-FileContributor: Henrique Moody <henriquemoody@gmail.com>
-->
# Length
- `Length(Validator $validator)`
Validates the length of the given input against a given validator.
```php
v::length(v::between(1, 5))->assert('abc');
// Validation passes successfully
v::length(v::greaterThan(5))->assert('abcdef');
// Validation passes successfully
v::length(v::lessThan(5))->assert('abc');
// Validation passes successfully
```
This validator can be used to validate the length of strings, arrays, and objects that implement the `Countable` interface.
```php
v::length(v::greaterThanOrEqual(3))->assert([1, 2, 3]);
// Validation passes successfully
v::length(v::equals(0))->assert(new SplPriorityQueue());
// Validation passes successfully
```
## Templates
### `Length::TEMPLATE_STANDARD`
Used when it's possible to get the length of the input.
| Mode | Template |
| ---------: | :------------ |
| `default` | The length of |
| `inverted` | The length of |
### `Length::TEMPLATE_WRONG_TYPE`
| Mode | Template |
| ---------: | :-------------------------------------------- |
| `default` | {{subject}} must be countable or a string |
| `inverted` | {{subject}} must not be countable or a string |
## Template as prefix
The template serves as a prefix to the template of the inner validator.
```php
v::length(v::equals(3))->assert('tulip');
// → The length of "tulip" must be equal to 3
v::not(v::length(v::equals(4)))->assert('rose');
// → The length of "rose" must not be equal to 4
```
### `Length::TEMPLATE_WRONG_TYPE`
Used when it's impossible to get the length of the input.
| Mode | Template |
| ---------- | ----------------------------------------------------- |
| `default` | {{subject}} must be a countable value or a string |
| `inverted` | {{subject}} must not be a countable value or a string |
## Template placeholders
| Placeholder | Description |
| ----------- | ---------------------------------------------------------------- |
| `subject` | The validated input or the custom validator name (if specified). |
## Categorization
- Comparisons
- Transformations
## Changelog
| Version | Description |
| ------: | :---------------------- |
| 3.0.0 | Became a transformation |
| 0.3.9 | Created |
## See Also
- [All](All.md)
- [Between](Between.md)
- [BetweenExclusive](BetweenExclusive.md)
- [Each](Each.md)
- [GreaterThan](GreaterThan.md)
- [GreaterThanOrEqual](GreaterThanOrEqual.md)
- [LessThan](LessThan.md)
- [LessThanOrEqual](LessThanOrEqual.md)
- [Max](Max.md)
- [Min](Min.md)