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

Commit 2599a75

Browse files
committed
feat(docs): added Email rule documentation
1 parent 57a16e9 commit 2599a75

File tree

6 files changed

+91
-6
lines changed

6 files changed

+91
-6
lines changed

docs/03x-rules-choice.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ For example, if `maxConstraint` is 2, the input array must have at most 2 values
7878

7979
### `message`
8080

81-
type `string` default: `The {{ name }} value is not a valid choice, {{ value }} given. Accepted values are: {{ constraints }}.`
81+
type: `string` default: `The {{ name }} value is not a valid choice, {{ value }} given. Accepted values are: {{ constraints }}.`
8282

8383
Message that will be shown if input value is not a valid choice.
8484

@@ -92,7 +92,7 @@ The following parameters are available:
9292

9393
### `multipleMessage`
9494

95-
type `string` default: `The {{ name }} value has one or more invalid choices, {{ value }} given. Accepted values are: {{ constraints }}.`
95+
type: `string` default: `The {{ name }} value has one or more invalid choices, {{ value }} given. Accepted values are: {{ constraints }}.`
9696

9797
Message that will be shown when `multiple` is `true` and at least one of the input array values is not a valid choice.
9898

@@ -106,7 +106,7 @@ The following parameters are available:
106106

107107
### `minMessage`
108108

109-
type `string` default: `The {{ name }} value must have at least {{ minConstraint }} choices, {{ numValues }} choices given.`
109+
type: `string` default: `The {{ name }} value must have at least {{ minConstraint }} choices, {{ numValues }} choices given.`
110110

111111
Message that will be shown when `multiple` is `true` and input array has fewer values than the defined in `minConstraint`.
112112

@@ -123,7 +123,7 @@ The following parameters are available:
123123

124124
### `maxMessage`
125125

126-
type `string` default: `The {{ name }} value must have at most {{ maxConstraint }} choices, {{ numValues }} choices given.`
126+
type: `string` default: `The {{ name }} value must have at most {{ maxConstraint }} choices, {{ numValues }} choices given.`
127127

128128
Message that will be shown when `multiple` is `true` and input array has more values than the defined in `maxConstraint`.
129129

docs/03x-rules-email.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Email
2+
3+
Validates that a value is a valid email address.
4+
5+
```php
6+
Email(
7+
string $mode = 'html5',
8+
?callable $normalizer = null,
9+
string $message = 'The {{ name }} value is not a valid email address, {{ value }} given.'
10+
);
11+
```
12+
13+
## Basic Usage
14+
15+
```php
16+
// html5 mode (default)
17+
Validator::email()->validate('test@example.com'); // true
18+
Validator::email()->validate('test@example'); // false
19+
20+
// html5-allow-no-tld mode
21+
Validator::email(mode: 'html5-allow-no-tld')->validate('test@example.com'); // true
22+
Validator::email(mode: 'html5-allow-no-tld')->validate('test@example'); // true
23+
```
24+
25+
> **Note**
26+
> An `UnexpectedValueException` will be thrown when a `mode` option is invalid.
27+
28+
## Options
29+
30+
### `mode`
31+
32+
type: `string` default: `html5`
33+
34+
Set this option to define the validation mode.
35+
36+
Available options are:
37+
38+
- `html5` uses the regular expression of an HTML5 email input element, but enforces it to have a TLD extension.
39+
- `html5-allow-no-tld` uses the regular expression of an HTML5 email input element, which allows addresses without a TLD extension.
40+
- `strict` validates an address according to the [RFC 5322](https://datatracker.ietf.org/doc/html/rfc5322) specification.
41+
42+
### `normalizer`
43+
44+
type: `callable` default: `null`
45+
46+
Allows to define a `callable` that will be applied to the value before checking if it is valid.
47+
48+
For example, use `trim`, or pass your own function, to ignore whitespace in the beginning or end of an email address:
49+
50+
```php
51+
Validator::email()->validate('test@example.com '); // false
52+
53+
Validator::email(normalizer: 'trim')->validate('test@example.com '); // true
54+
Validator::email(normalizer: fn($value) => trim($value))->validate('test@example.com '); // true
55+
```
56+
57+
### `message`
58+
59+
type `string` default: `The {{ name }} value is not a valid email address, {{ value }} given.`
60+
61+
Message that will be shown if the input value is not a valid email address.
62+
63+
The following parameters are available:
64+
65+
| Parameter | Description |
66+
|---------------|---------------------------|
67+
| `{{ value }}` | The current invalid value |
68+
| `{{ name }}` | Name of the invalid value |
69+
| `{{ mode }}` | Selected validation mode |
70+
71+
## Changelog
72+
73+
- `0.6.0` Created

docs/03x-rules-timezone.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ Check the [official country codes](https://en.wikipedia.org/wiki/ISO_3166-1#Curr
7575

7676
### `message`
7777

78-
type `string` default: `The {{ name }} value is not a valid timezone, {{ value }} given.`
78+
type: `string` default: `The {{ name }} value is not a valid timezone, {{ value }} given.`
7979

8080
Message that will be shown if the input value is not a valid timezone.
8181

docs/03x-rules-type.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ Available character type constraints:
7777

7878
### `message`
7979

80-
type `string` default: `The {{ name }} value should be of type {{ constraint }}, {{ value }} given.`
80+
type: `string` default: `The {{ name }} value should be of type {{ constraint }}, {{ value }} given.`
8181

8282
Message that will be shown if input value is not of a specific type.
8383

src/ChainedValidatorInterface.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ public function eachValue(
3333
string $message = 'At key {{ key }}: {{ message }}'
3434
): ChainedValidatorInterface&Validator;
3535

36+
static function email(
37+
string $mode = 'html5',
38+
?callable $normalizer = null,
39+
string $message = 'The {{ name }} value is not a valid email address, {{ value }} given.'
40+
): ChainedValidatorInterface&Validator;
41+
3642
public function greaterThan(
3743
mixed $constraint,
3844
string $message = 'The {{ name }} value should be greater than {{ constraint }}, {{ value }} given.'

src/StaticValidatorInterface.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ public static function eachValue(
3232
string $message = 'At key {{ key }}: {{ message }}'
3333
): ChainedValidatorInterface&Validator;
3434

35+
public static function email(
36+
string $mode = 'html5',
37+
?callable $normalizer = null,
38+
string $message = 'The {{ name }} value is not a valid email address, {{ value }} given.'
39+
): ChainedValidatorInterface&Validator;
40+
3541
public static function greaterThan(
3642
mixed $constraint,
3743
string $message = 'The {{ name }} value should be greater than {{ constraint }}, {{ value }} given.'

0 commit comments

Comments
 (0)