Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit c922cd7

Browse files
committed
feat: added Range rule documentation
1 parent a22bd34 commit c922cd7

1 file changed

Lines changed: 64 additions & 0 deletions

File tree

docs/03-rules-range.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Range
2+
3+
Validates that a value is between a minimum and maximum value.
4+
Can compare between strings, numbers and dates.
5+
6+
## Basic Usage
7+
8+
Values equal to the defined minimum and maximum constraints are considered valid.
9+
10+
```php
11+
Validator::range(1, 20)->validate(10); // true
12+
Validator::range(1, 20)->validate(1); // true
13+
14+
Validator::range(1.5, 3.5)->validate(2.5); // true
15+
Validator::range(1.5, 3.5)->validate(3.5); // true
16+
17+
Validator::range('alpha', 'gamma')->validate('beta'); // true
18+
Validator::range('alpha', 'gamma')->validate('alpha'); // true
19+
20+
Validator::range(new DateTime('yesterday'), new DateTime('tomorrow'))->validate(new DateTime('today')); // true
21+
Validator::range(new DateTime('yesterday'), new DateTime('tomorrow'))->validate(new DateTime('tomorrow')); // true
22+
```
23+
24+
> **Note**
25+
> String comparison is case-sensitive, meaning that comparing `'hello'` with `'Hello'` is different.
26+
> Check [`strcmp`](https://www.php.net/manual/en/function.strcmp.php) for more information.
27+
28+
> **Note**
29+
> An `UnexpectedValueException` will be thrown when trying to compare incomparable values, like a `string` with an `int`
30+
31+
> **Note**
32+
> An `UnexpectedValueException` will be thrown when the `minConstraint` value is greater than or equal to the `maxConstraint` value.
33+
34+
## Options
35+
36+
### `minConstraint`
37+
38+
type: `mixed` `required`
39+
40+
It defines the minimum range value.
41+
Can be a `string`, `int`, `float` or `DateTimeInterface` object.
42+
43+
### `maxConstraint`
44+
45+
type: `mixed` `required`
46+
47+
It defines the maximum range value.
48+
Can be a `string`, `int`, `float` or `DateTimeInterface` object.
49+
50+
### `message`
51+
52+
type: `string` default: `The "{{ name }}" value should be between "{{ minConstraint }}" and "{{ maxConstraint }}", "{{ value }}" given.`
53+
54+
Message that will be shown if the value is not between the minimum and maximum constraint values.
55+
Check the [Custom Messages]() section for more information.
56+
57+
The following parameters are available:
58+
59+
| Parameter | Description |
60+
|-----------------------|-----------------------------------|
61+
| `{{ value }}` | The current invalid value |
62+
| `{{ name }}` | Name of the value being validated |
63+
| `{{ minConstraint }}` | The minimum range value |
64+
| `{{ maxConstraint }}` | The maximum range value |

0 commit comments

Comments
 (0)