- 
                Notifications
    You must be signed in to change notification settings 
- Fork 4.8k
[11.x] Document the fluent email Rule validation #10112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Closed
      
      
    
  
     Closed
                    Changes from 2 commits
      Commits
    
    
            Show all changes
          
          
            14 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      3883628
              
                Add Validating Emails docs
              
              
                SanderMuller e7526ed
              
                Update validation.md
              
              
                SanderMuller 8cad670
              
                Apply review feedback
              
              
                SanderMuller 6f7f34e
              
                Update validation.md
              
              
                SanderMuller a1a541e
              
                Update validation.md
              
              
                SanderMuller 08e567c
              
                Neaten
              
              
                SanderMuller e4bc18c
              
                Update validation.md
              
              
                SanderMuller d85df2b
              
                Update validation.md
              
              
                SanderMuller 8c7a5f6
              
                Update validation.md
              
              
                SanderMuller 4da4cee
              
                Update validation.md
              
              
                SanderMuller af2f2d5
              
                Update validation.md
              
              
                SanderMuller 10e3baf
              
                Update validation.md
              
              
                SanderMuller 42445e9
              
                Update validation.md
              
              
                SanderMuller d18ecca
              
                Update validation.md
              
              
                SanderMuller File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|  | @@ -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) | ||||||
|  | @@ -1240,6 +1241,8 @@ The `filter` validator, which uses PHP's `filter_var` function, ships with Larav | |||||
| > [!WARNING] | ||||||
| > The `dns` and `spoof` validators require the PHP `intl` extension. | ||||||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could link this: 
        Suggested change
       
 | ||||||
|  | ||||||
| Since email validation can be quite complex, you may use (Rule::email(#validating-emails) to fluently construct the rule. | ||||||
|         
                  SanderMuller marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||||||
|  | ||||||
| <a name="rule-ends-with"></a> | ||||||
| #### ends_with:_foo_,_bar_,... | ||||||
|  | ||||||
|  | @@ -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: | ||||||
|         
                  SanderMuller marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||||||
| 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: | ||||||
|         
                  SanderMuller marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||||||
|  | ||||||
| ```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. | ||||||
|         
                  SanderMuller marked this conversation as resolved.
              Show resolved
            Hide resolved | ||||||
|  | ||||||
| <a name="defining-default-email-rules"></a> | ||||||
|  | ||||||
| Defining Default Email Rules | ||||||
|         
                  SanderMuller marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||||||
| 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: | ||||||
|         
                  SanderMuller marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||||||
|  | ||||||
| ```php | ||||||
| use Illuminate\Validation\Rules\Email; | ||||||
|  | ||||||
| /** | ||||||
| * Bootstrap any application services. | ||||||
| */ | ||||||
| public function boot(): void | ||||||
| { | ||||||
| Email::defaults(function () { | ||||||
| $rule = (new Email())->rfcCompliant(strict: true)->preventSpoofing(), | ||||||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be  | ||||||
|  | ||||||
| return $this->app->isProduction() | ||||||
| ? $rule->validateMxRecord() | ||||||
| : $rule; | ||||||
|         
                  SanderMuller marked this conversation as resolved.
              Show resolved
            Hide resolved | ||||||
| }); | ||||||
| } | ||||||
| ``` | ||||||
| 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 | ||||||
|  | ||||||
|  | ||||||
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
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: