Skip to content
81 changes: 81 additions & 0 deletions validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
- [Error Message Indexes and Positions](#error-message-indexes-and-positions)
- [Validating Files](#validating-files)
- [Validating Passwords](#validating-passwords)
- [Validating Emails](#validating-emails)
- [Custom Validation Rules](#custom-validation-rules)
- [Using Rule Objects](#using-rule-objects)
- [Using Closures](#using-closures)
Expand Down Expand Up @@ -1240,6 +1241,8 @@ The `filter` validator, which uses PHP's `filter_var` function, ships with Larav
> [!WARNING]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might want to link this:

- The `filter` validator, which uses PHP's `filter_var` function, ships with Laravel and was Laravel's default email validation behavior prior to Laravel version 5.8.
+ The `filter` validator, which uses PHP's [`filter_var` function](https://www.php.net/manual/en/filter.constants.php#constant.filter-validate-email), ships with Laravel and was Laravel's default email validation behavior prior to Laravel version 5.8.
 
[!WARNING]

> The `dns` and `spoof` validators require the PHP `intl` extension.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could link this:

Suggested change
> The `dns` and `spoof` validators require the PHP `intl` extension.
> The `dns` and `spoof` validators require the [PHP `intl` extension](https://www.php.net/manual/de/book.intl.php).


Since email validation can be quite complex, you may use (Rule::email(#validating-emails) to fluently construct the rule.

<a name="rule-ends-with"></a>
#### ends_with:_foo_,_bar_,...

Expand Down Expand Up @@ -2241,6 +2244,84 @@ Occasionally, you may want to attach additional validation rules to your default
// ...
});

<a name="validating-emails"></a>
## Validating Emails

Laravel provides a variety of validation rules that may be used to validate uploaded files, such as `rfc`, `strict` and `dns`. While you are free to specify these rules individually when validating emails, Laravel also offers a fluent file validation rule builder that you may find convenient:
To ensure that emails are valid according to your application's requirements, you may use `Rule` class to fluently define the rule via ` Rule::email()`:

```php
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;

$validator = Validator::make($request->all(), [
'email' => ['required', Rule::email()],
]);
```
The Email rule object allows you to easily customize how emails are validated for your application, such as specifying that emails require RFC compliance, DNS checks, native PHP filtering, or spoof detection:

```php
// Basic RFC compliance...
Rule::email()->rfcCompliant();

// Strict RFC compliance...
Rule::email()->rfcCompliant(strict: true);

// Check for valid MX records...
Rule::email()->validateMxRecord();

// Prevent spoofing...
Rule::email()->preventSpoofing();
```
Of course, you may chain all the methods in the examples above:

```php
Rule::email()
->rfcCompliant(strict: true)
->validateMxRecord()
->preventSpoofing();
```

> [!WARNING]
> The `validateMxRecord()` and `preventSpoofing()` validators require the PHP `intl` extension.

<a name="defining-default-email-rules"></a>

Defining Default Email Rules
You may find it convenient to specify the default validation rules for emails in a single location of your application. You can easily accomplish this using the Email::defaults method, which accepts a closure. The closure given to the defaults method should return the default configuration of the Email rule. Typically, the defaults rule should be called within the boot method of one of your application's service providers:

```php
use Illuminate\Validation\Rules\Email;

/**
* Bootstrap any application services.
*/
public function boot(): void
{
Email::defaults(function () {
$rule = (new Email())->rfcCompliant(strict: true)->preventSpoofing(),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be ; instead of , at the end


return $this->app->isProduction()
? $rule->validateMxRecord()
: $rule;
});
}
```
Then, when you would like to apply the default rules to a particular email undergoing validation, you may invoke the defaults method with no arguments:

```php
'email' => ['required', Email::defaults()],
```
Occasionally, you may want to attach additional validation rules to your default email validation rules. You may use the rules method to accomplish this:

```php
Email::defaults(function () {
return (new Email())->rfcCompliant(strict: true)
->preventSpoofing()
->rules(['ends_with:@example.com']);
});
```

<a name="custom-validation-rules"></a>
## Custom Validation Rules

Expand Down