|
| 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 |
0 commit comments