2015-01-30 09:40:06 +01:00
|
|
|
# Length
|
|
|
|
|
2017-03-27 14:47:07 +02:00
|
|
|
- `Length(int $min, int $max)`
|
|
|
|
- `Length(int $min, null)`
|
|
|
|
- `Length(null, int $max)`
|
|
|
|
- `Length(int $min, int $max, bool $inclusive)`
|
2015-01-30 09:40:06 +01:00
|
|
|
|
2018-09-04 13:31:58 +02:00
|
|
|
Validates the length of the given input.
|
|
|
|
|
|
|
|
Most simple example:
|
2015-01-30 09:40:06 +01:00
|
|
|
|
|
|
|
```php
|
2015-10-20 02:53:25 +02:00
|
|
|
v::stringType()->length(1, 5)->validate('abc'); // true
|
2015-01-30 09:40:06 +01:00
|
|
|
```
|
|
|
|
|
|
|
|
You can also validate only minimum length:
|
|
|
|
|
|
|
|
```php
|
2015-10-07 16:52:03 +02:00
|
|
|
v::stringType()->length(5, null)->validate('abcdef'); // true
|
2015-01-30 09:40:06 +01:00
|
|
|
```
|
|
|
|
|
|
|
|
Only maximum length:
|
|
|
|
|
|
|
|
```php
|
2015-10-07 16:52:03 +02:00
|
|
|
v::stringType()->length(null, 5)->validate('abc'); // true
|
2015-01-30 09:40:06 +01:00
|
|
|
```
|
|
|
|
|
|
|
|
The type as the first validator in a chain is a good practice,
|
|
|
|
since length accepts many types:
|
|
|
|
|
|
|
|
```php
|
2015-10-20 02:53:25 +02:00
|
|
|
v::arrayVal()->length(1, 5)->validate(['foo', 'bar']); // true
|
2015-01-30 09:40:06 +01:00
|
|
|
```
|
|
|
|
|
|
|
|
A third parameter may be passed to validate the passed values inclusive:
|
|
|
|
|
|
|
|
```php
|
2015-10-20 02:53:25 +02:00
|
|
|
v::stringType()->length(1, 5, true)->validate('a'); // true
|
2015-01-30 09:40:06 +01:00
|
|
|
```
|
|
|
|
|
|
|
|
Message template for this validator includes `{{minValue}}` and `{{maxValue}}`.
|
|
|
|
|
2017-03-27 22:04:13 +02:00
|
|
|
## Changelog
|
|
|
|
|
|
|
|
Version | Description
|
|
|
|
--------|-------------
|
|
|
|
0.3.9 | Created
|
|
|
|
|
2015-08-20 06:38:41 +02:00
|
|
|
***
|
2015-01-30 09:40:06 +01:00
|
|
|
See also:
|
|
|
|
|
2017-03-28 11:53:14 +02:00
|
|
|
- [Between](Between.md)
|
2018-12-11 13:31:50 +01:00
|
|
|
- [Min](Min.md)
|