Lỗi not available in current platfom trong steam năm 2024

We've brought together everything from the developer, your friends, and the community into a single place to make it easier to keep up with what's happening outside the game.

Post Game Summary

The post game summary appears at the top of your game details page and makes it easy to see your achievements, screenshots, and trading cards earned during your most recent play session.

Friends Who Play

Want to know who plays this game? Maybe you’re stuck and need some advice, trying to build a party, or just want to know you’re not alone in the universe – fear not your friends are here now.

Friends' Posts

Posts from friends including shared screenshots, achievements and status updates now appear in the game details feed. Leave a comment or like a post to keep the conversation going without leaving the library.

Community Content

Steam users are already creating amazing fan art, game guides, videos, and memes for some of your favorite games. We spared no pixels bringing that content to the bottom of your game page so you can easily see what’s new in the community and join the conversation.

Laravel provides several different approaches to validate your application's incoming data. It is most common to use the


  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    39 method available on all incoming HTTP requests. However, we will discuss other approaches to validation as well.

    Laravel includes a wide variety of convenient validation rules that you may apply to data, even providing the ability to validate if values are unique in a given database table. We'll cover each of these validation rules in detail so that you are familiar with all of Laravel's validation features.

    Validation Quickstart

    To learn about Laravel's powerful validation features, let's look at a complete example of validating a form and displaying the error messages back to the user. By reading this high-level overview, you'll be able to gain a good general understanding of how to validate incoming request data using Laravel:

    Defining the Routes

    First, let's assume we have the following routes defined in our

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    40 file:

    
    use App\Http\Controllers\PostController;
    
    Route::get('/post/create', [PostController::class, 'create']);
    
    Route::post('/post', [PostController::class, 'store']);
    
    

    The

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    41 route will display a form for the user to create a new blog post, while the

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    42 route will store the new blog post in the database.

    Creating the Controller

    Next, let's take a look at a simple controller that handles incoming requests to these routes. We'll leave the

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    43 method empty for now:

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    Writing the Validation Logic

    Now we are ready to fill in our

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    43 method with the logic to validate the new blog post. To do this, we will use the

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    39 method provided by the

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    46 object. If the validation rules pass, your code will keep executing normally; however, if validation fails, an

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    47 exception will be thrown and the proper error response will automatically be sent back to the user.

    If validation fails during a traditional HTTP request, a redirect response to the previous URL will be generated. If the incoming request is an XHR request, a will be returned.

    To get a better understanding of the

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    39 method, let's jump back into the

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    43 method:

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    As you can see, the validation rules are passed into the

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    39 method. Don't worry - all available validation rules are . Again, if the validation fails, the proper response will automatically be generated. If the validation passes, our controller will continue executing normally.

    Alternatively, validation rules may be specified as arrays of rules instead of a single

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    51 delimited string:

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    In addition, you may use the

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    52 method to validate a request and store any error messages within a :

    
    $validatedData = $request->validateWithBag('post', [
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    Stopping on First Validation Failure

    Sometimes you may wish to stop running validation rules on an attribute after the first validation failure. To do so, assign the

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    53 rule to the attribute:

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    In this example, if the

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    54 rule on the

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    55 attribute fails, the

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    56 rule will not be checked. Rules will be validated in the order they are assigned.

    A Note on Nested Attributes

    If the incoming HTTP request contains "nested" field data, you may specify these fields in your validation rules using "dot" syntax:

    
    $request->validate([
    
        'title' => 'required|unique:posts|max:255',
    
        'author.name' => 'required',
    
        'author.description' => 'required',
    
    ]);
    
    

    On the other hand, if your field name contains a literal period, you can explicitly prevent this from being interpreted as "dot" syntax by escaping the period with a backslash:

    
    $request->validate([
    
        'title' => 'required|unique:posts|max:255',
    
        'v1\.0' => 'required',
    
    ]);
    
    

    Displaying the Validation Errors

    So, what if the incoming request fields do not pass the given validation rules? As mentioned previously, Laravel will automatically redirect the user back to their previous location. In addition, all of the validation errors and will automatically be .

    An

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    57 variable is shared with all of your application's views by the

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    58 middleware, which is provided by the

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    59 middleware group. When this middleware is applied an

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    57 variable will always be available in your views, allowing you to conveniently assume the

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    57 variable is always defined and can be safely used. The

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    57 variable will be an instance of

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    63. For more information on working with this object, .

    So, in our example, the user will be redirected to our controller's

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    64 method when validation fails, allowing us to display the error messages in the view:

    
    
    
    

    Create Post

    @if ($errors->any())
      @foreach ($errors->all() as $error)
    • {{ $error }}
    • @endforeach
    @endif

    Customizing the Error Messages

    Laravel's built-in validation rules each have an error message that is located in your application's

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    65 file. If your application does not have a

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    66 directory, you may instruct Laravel to create it using the

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    67 Artisan command.

    Within the

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    65 file, you will find a translation entry for each validation rule. You are free to change or modify these messages based on the needs of your application.

    In addition, you may copy this file to another language directory to translate the messages for your application's language. To learn more about Laravel localization, check out the complete localization documentation.

    [!WARNING]

    By default, the Laravel application skeleton does not include the

    namespace App\Http\Controllers;

    use Illuminate\Http\RedirectResponse;

    use Illuminate\Http\Request;

    use Illuminate\View\View;

    class PostController extends Controller

    {

    / Show the form to create a new blog post. /

    public function create(): View

    {

    return view('post.create');

    }

    /
    Store a new blog post. /

    public function store(Request $request): RedirectResponse

    {

    // Validate and store the blog post...

    $post = / ... */

    return to_route('post.show', ['post' => $post->id]);

    }

    }

    66 directory. If you would like to customize Laravel's language files, you may publish them via the

    namespace App\Http\Controllers;

    use Illuminate\Http\RedirectResponse;

    use Illuminate\Http\Request;

    use Illuminate\View\View;

    class PostController extends Controller

    {

    / Show the form to create a new blog post. /

    public function create(): View

    {

    return view('post.create');

    }

    /
    Store a new blog post. /

    public function store(Request $request): RedirectResponse

    {

    // Validate and store the blog post...

    $post = / ... */

    return to_route('post.show', ['post' => $post->id]);

    }

    }

    67 Artisan command.

    XHR Requests and Validation

    In this example, we used a traditional form to send data to the application. However, many applications receive XHR requests from a JavaScript powered frontend. When using the

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    39 method during an XHR request, Laravel will not generate a redirect response. Instead, Laravel generates a . This JSON response will be sent with a 422 HTTP status code.

    The

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    72 Directive

    You may use the

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    72 Blade directive to quickly determine if validation error messages exist for a given attribute. Within an

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    72 directive, you may echo the

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    75 variable to display the error message:

    
    
    
    
    
    
    
    @error('title')
    
        
    {{ $message }}
    @enderror

    If you are using , you may pass the name of the error bag as the second argument to the

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    72 directive:

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    0

    Repopulating Forms

    When Laravel generates a redirect response due to a validation error, the framework will automatically . This is done so that you may conveniently access the input during the next request and repopulate the form that the user attempted to submit.

    To retrieve flashed input from the previous request, invoke the

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    77 method on an instance of

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    46. The

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    77 method will pull the previously flashed input data from the session:

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    1

    Laravel also provides a global

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    77 helper. If you are displaying old input within a Blade template, it is more convenient to use the

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    77 helper to repopulate the form. If no old input exists for the given field,

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    82 will be returned:

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    2

    A Note on Optional Fields

    By default, Laravel includes the

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    83 and

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    84 middleware in your application's global middleware stack. Because of this, you will often need to mark your "optional" request fields as

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    85 if you do not want the validator to consider

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    82 values as invalid. For example:

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    3

    In this example, we are specifying that the

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    87 field may be either

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    82 or a valid date representation. If the

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    85 modifier is not added to the rule definition, the validator would consider

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    82 an invalid date.

    Validation Error Response Format

    When your application throws a

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    47 exception and the incoming HTTP request is expecting a JSON response, Laravel will automatically format the error messages for you and return a

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    92 HTTP response.

    Below, you can review an example of the JSON response format for validation errors. Note that nested error keys are flattened into "dot" notation format:

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    4

    Form Request Validation

    Creating Form Requests

    For more complex validation scenarios, you may wish to create a "form request". Form requests are custom request classes that encapsulate their own validation and authorization logic. To create a form request class, you may use the

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    93 Artisan CLI command:

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    5

    The generated form request class will be placed in the

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    94 directory. If this directory does not exist, it will be created when you run the

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    93 command. Each form request generated by Laravel has two methods:

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    96 and

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    97.

    As you might have guessed, the

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    96 method is responsible for determining if the currently authenticated user can perform the action represented by the request, while the

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    97 method returns the validation rules that should apply to the request's data:

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    6

    [!NOTE]

    You may type-hint any dependencies you require within the

    namespace App\Http\Controllers;

    use Illuminate\Http\RedirectResponse;

    use Illuminate\Http\Request;

    use Illuminate\View\View;

    class PostController extends Controller

    {

    / Show the form to create a new blog post. /

    public function create(): View

    {

    return view('post.create');

    }

    /
    Store a new blog post. /

    public function store(Request $request): RedirectResponse

    {

    // Validate and store the blog post...

    $post = / ... */

    return to_route('post.show', ['post' => $post->id]);

    }

    }

    97 method's signature. They will automatically be resolved via the Laravel service container.

    So, how are the validation rules evaluated? All you need to do is type-hint the request on your controller method. The incoming form request is validated before the controller method is called, meaning you do not need to clutter your controller with any validation logic:

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    7

    If validation fails, a redirect response will be generated to send the user back to their previous location. The errors will also be flashed to the session so they are available for display. If the request was an XHR request, an HTTP response with a 422 status code will be returned to the user including a .

    [!NOTE] Need to add real-time form request validation to your Inertia powered Laravel frontend? Check out Laravel Precognition.

    Performing Additional Validation

    Sometimes you need to perform additional validation after your initial validation is complete. You can accomplish this using the form request's

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    01 method.

    The

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    01 method should return an array of callables or closures which will be invoked after validation is complete. The given callables will receive an

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    03 instance, allowing you to raise additional error messages if necessary:

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    8

    As noted, the array returned by the

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    01 method may also contain invokable classes. The

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    05 method of these classes will receive an

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    03 instance:

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    9

    Stopping on the First Validation Failure

    By adding a

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    07 property to your request class, you may inform the validator that it should stop validating all attributes once a single validation failure has occurred:

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    0

    Customizing the Redirect Location

    As previously discussed, a redirect response will be generated to send the user back to their previous location when form request validation fails. However, you are free to customize this behavior. To do so, define a

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    08 property on your form request:

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    1

    Or, if you would like to redirect users to a named route, you may define a

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    09 property instead:

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    2

    Authorizing Form Requests

    The form request class also contains an

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    96 method. Within this method, you may determine if the authenticated user actually has the authority to update a given resource. For example, you may determine if a user actually owns a blog comment they are attempting to update. Most likely, you will interact with your authorization gates and policies within this method:

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    3

    Since all form requests extend the base Laravel request class, we may use the

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    11 method to access the currently authenticated user. Also, note the call to the

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    12 method in the example above. This method grants you access to the URI parameters defined on the route being called, such as the

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    13 parameter in the example below:

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    4

    Therefore, if your application is taking advantage of , your code may be made even more succinct by accessing the resolved model as a property of the request:

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    5

    If the

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    96 method returns

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    15, an HTTP response with a 403 status code will automatically be returned and your controller method will not execute.

    If you plan to handle authorization logic for the request in another part of your application, you may remove the

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    96 method completely, or simply return

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    17:

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    6

    [!NOTE]

    You may type-hint any dependencies you need within the

    namespace App\Http\Controllers;

    use Illuminate\Http\RedirectResponse;

    use Illuminate\Http\Request;

    use Illuminate\View\View;

    class PostController extends Controller

    {

    / Show the form to create a new blog post. /

    public function create(): View

    {

    return view('post.create');

    }

    /
    Store a new blog post. /

    public function store(Request $request): RedirectResponse

    {

    // Validate and store the blog post...

    $post = / ... */

    return to_route('post.show', ['post' => $post->id]);

    }

    }

    96 method's signature. They will automatically be resolved via the Laravel service container.

    Customizing the Error Messages

    You may customize the error messages used by the form request by overriding the

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    19 method. This method should return an array of attribute / rule pairs and their corresponding error messages:

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    7

    Customizing the Validation Attributes

    Many of Laravel's built-in validation rule error messages contain an

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    20 placeholder. If you would like the

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    20 placeholder of your validation message to be replaced with a custom attribute name, you may specify the custom names by overriding the

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    22 method. This method should return an array of attribute / name pairs:

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    8

    Preparing Input for Validation

    If you need to prepare or sanitize any data from the request before you apply your validation rules, you may use the

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    23 method:

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    9

    Likewise, if you need to normalize any request data after validation is complete, you may use the

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    24 method:

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    0

    Manually Creating Validators

    If you do not want to use the

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    39 method on the request, you may create a validator instance manually using the

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    26 facade. The

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    27 method on the facade generates a new validator instance:

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    1

    The first argument passed to the

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    27 method is the data under validation. The second argument is an array of the validation rules that should be applied to the data.

    After determining whether the request validation failed, you may use the

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    29 method to flash the error messages to the session. When using this method, the

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    57 variable will automatically be shared with your views after redirection, allowing you to easily display them back to the user. The

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    29 method accepts a validator, a

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    32, or a PHP

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    33.

    Stopping on First Validation Failure

    The

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    07 method will inform the validator that it should stop validating all attributes once a single validation failure has occurred:

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    2

    Automatic Redirection

    If you would like to create a validator instance manually but still take advantage of the automatic redirection offered by the HTTP request's

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    39 method, you may call the

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    39 method on an existing validator instance. If validation fails, the user will automatically be redirected or, in the case of an XHR request, a :

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    3

    You may use the

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    52 method to store the error messages in a if validation fails:

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    4

    Named Error Bags

    If you have multiple forms on a single page, you may wish to name the

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    32 containing the validation errors, allowing you to retrieve the error messages for a specific form. To achieve this, pass a name as the second argument to

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    29:

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    5

    You may then access the named

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    32 instance from the

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    57 variable:

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    6

    Customizing the Error Messages

    If needed, you may provide custom error messages that a validator instance should use instead of the default error messages provided by Laravel. There are several ways to specify custom messages. First, you may pass the custom messages as the third argument to the

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    42 method:

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    7

    In this example, the

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    20 placeholder will be replaced by the actual name of the field under validation. You may also utilize other placeholders in validation messages. For example:

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    8

    Specifying a Custom Message for a Given Attribute

    Sometimes you may wish to specify a custom error message only for a specific attribute. You may do so using "dot" notation. Specify the attribute's name first, followed by the rule:

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    9

    Specifying Custom Attribute Values

    Many of Laravel's built-in error messages include an

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    20 placeholder that is replaced with the name of the field or attribute under validation. To customize the values used to replace these placeholders for specific fields, you may pass an array of custom attributes as the fourth argument to the

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    42 method:

    
    $validatedData = $request->validateWithBag('post', [
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    0

    Performing Additional Validation

    Sometimes you need to perform additional validation after your initial validation is complete. You can accomplish this using the validator's

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    01 method. The

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    01 method accepts a closure or an array of callables which will be invoked after validation is complete. The given callables will receive an

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    03 instance, allowing you to raise additional error messages if necessary:

    
    $validatedData = $request->validateWithBag('post', [
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    1

    As noted, the

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    01 method also accepts an array of callables, which is particularly convenient if your "after validation" logic is encapsulated in invokable classes, which will receive an

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    03 instance via their

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    05 method:

    
    $validatedData = $request->validateWithBag('post', [
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    2

    Working With Validated Input

    After validating incoming request data using a form request or a manually created validator instance, you may wish to retrieve the incoming request data that actually underwent validation. This can be accomplished in several ways. First, you may call the

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    52 method on a form request or validator instance. This method returns an array of the data that was validated:

    
    $validatedData = $request->validateWithBag('post', [
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    3

    Alternatively, you may call the

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    53 method on a form request or validator instance. This method returns an instance of

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    54. This object exposes

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    55,

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    56, and

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    57 methods to retrieve a subset of the validated data or the entire array of validated data:

    
    $validatedData = $request->validateWithBag('post', [
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    4

    In addition, the

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    54 instance may be iterated over and accessed like an array:

    
    $validatedData = $request->validateWithBag('post', [
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    5

    If you would like to add additional fields to the validated data, you may call the

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    59 method:

    
    $validatedData = $request->validateWithBag('post', [
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    6

    If you would like to retrieve the validated data as a collection instance, you may call the

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    60 method:

    
    $validatedData = $request->validateWithBag('post', [
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    7

    Working With Error Messages

    After calling the

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    61 method on a

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    26 instance, you will receive an

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    63 instance, which has a variety of convenient methods for working with error messages. The

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    57 variable that is automatically made available to all views is also an instance of the

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    32 class.

    Retrieving the First Error Message for a Field

    To retrieve the first error message for a given field, use the

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    66 method:

    
    $validatedData = $request->validateWithBag('post', [
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    8

    Retrieving All Error Messages for a Field

    If you need to retrieve an array of all the messages for a given field, use the

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    67 method:

    
    $validatedData = $request->validateWithBag('post', [
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    9

    If you are validating an array form field, you may retrieve all of the messages for each of the array elements using the

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    68 character:

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    0

    Retrieving All Error Messages for All Fields

    To retrieve an array of all messages for all fields, use the

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    57 method:

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    1

    Determining if Messages Exist for a Field

    The

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    70 method may be used to determine if any error messages exist for a given field:

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    2

    Specifying Custom Messages in Language Files

    Laravel's built-in validation rules each have an error message that is located in your application's

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    65 file. If your application does not have a

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    66 directory, you may instruct Laravel to create it using the

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    67 Artisan command.

    Within the

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    65 file, you will find a translation entry for each validation rule. You are free to change or modify these messages based on the needs of your application.

    In addition, you may copy this file to another language directory to translate the messages for your application's language. To learn more about Laravel localization, check out the complete localization documentation.

    [!WARNING]

    By default, the Laravel application skeleton does not include the

    namespace App\Http\Controllers;

    use Illuminate\Http\RedirectResponse;

    use Illuminate\Http\Request;

    use Illuminate\View\View;

    class PostController extends Controller

    {

    / Show the form to create a new blog post. /

    public function create(): View

    {

    return view('post.create');

    }

    /
    Store a new blog post. /

    public function store(Request $request): RedirectResponse

    {

    // Validate and store the blog post...

    $post = / ... */

    return to_route('post.show', ['post' => $post->id]);

    }

    }

    66 directory. If you would like to customize Laravel's language files, you may publish them via the

    namespace App\Http\Controllers;

    use Illuminate\Http\RedirectResponse;

    use Illuminate\Http\Request;

    use Illuminate\View\View;

    class PostController extends Controller

    {

    / Show the form to create a new blog post. /

    public function create(): View

    {

    return view('post.create');

    }

    /
    Store a new blog post. /

    public function store(Request $request): RedirectResponse

    {

    // Validate and store the blog post...

    $post = / ... */

    return to_route('post.show', ['post' => $post->id]);

    }

    }

    67 Artisan command.

    Custom Messages for Specific Attributes

    You may customize the error messages used for specified attribute and rule combinations within your application's validation language files. To do so, add your message customizations to the

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    77 array of your application's

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    78 language file:

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    3

    Specifying Attributes in Language Files

    Many of Laravel's built-in error messages include an

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    20 placeholder that is replaced with the name of the field or attribute under validation. If you would like the

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    20 portion of your validation message to be replaced with a custom value, you may specify the custom attribute name in the

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    22 array of your

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    78 language file:

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    4

    [!WARNING]

    By default, the Laravel application skeleton does not include the

    namespace App\Http\Controllers;

    use Illuminate\Http\RedirectResponse;

    use Illuminate\Http\Request;

    use Illuminate\View\View;

    class PostController extends Controller

    {

    / Show the form to create a new blog post. /

    public function create(): View

    {

    return view('post.create');

    }

    /
    Store a new blog post. /

    public function store(Request $request): RedirectResponse

    {

    // Validate and store the blog post...

    $post = / ... */

    return to_route('post.show', ['post' => $post->id]);

    }

    }

    66 directory. If you would like to customize Laravel's language files, you may publish them via the

    namespace App\Http\Controllers;

    use Illuminate\Http\RedirectResponse;

    use Illuminate\Http\Request;

    use Illuminate\View\View;

    class PostController extends Controller

    {

    / Show the form to create a new blog post. /

    public function create(): View

    {

    return view('post.create');

    }

    /
    Store a new blog post. /

    public function store(Request $request): RedirectResponse

    {

    // Validate and store the blog post...

    $post = / ... */

    return to_route('post.show', ['post' => $post->id]);

    }

    }

    67 Artisan command.

    Specifying Values in Language Files

    Some of Laravel's built-in validation rule error messages contain a

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    85 placeholder that is replaced with the current value of the request attribute. However, you may occasionally need the

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    85 portion of your validation message to be replaced with a custom representation of the value. For example, consider the following rule that specifies that a credit card number is required if the

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    87 has a value of

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    88:

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    5

    If this validation rule fails, it will produce the following error message:

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    6

    Instead of displaying

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    88 as the payment type value, you may specify a more user-friendly value representation in your

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    78 language file by defining a

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    91 array:

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    7

    [!WARNING]

    By default, the Laravel application skeleton does not include the

    namespace App\Http\Controllers;

    use Illuminate\Http\RedirectResponse;

    use Illuminate\Http\Request;

    use Illuminate\View\View;

    class PostController extends Controller

    {

    / Show the form to create a new blog post. /

    public function create(): View

    {

    return view('post.create');

    }

    /
    Store a new blog post. /

    public function store(Request $request): RedirectResponse

    {

    // Validate and store the blog post...

    $post = / ... */

    return to_route('post.show', ['post' => $post->id]);

    }

    }

    66 directory. If you would like to customize Laravel's language files, you may publish them via the

    namespace App\Http\Controllers;

    use Illuminate\Http\RedirectResponse;

    use Illuminate\Http\Request;

    use Illuminate\View\View;

    class PostController extends Controller

    {

    / Show the form to create a new blog post. /

    public function create(): View

    {

    return view('post.create');

    }

    /
    Store a new blog post. /

    public function store(Request $request): RedirectResponse

    {

    // Validate and store the blog post...

    $post = / ... */

    return to_route('post.show', ['post' => $post->id]);

    }

    }

    67 Artisan command.

    After defining this value, the validation rule will produce the following error message:

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    8

    Available Validation Rules

    Below is a list of all available validation rules and their function:

    accepted

    The field under validation must be

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    94,

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    95,

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    96,

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    97,

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    17, or

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    99. This is useful for validating "Terms of Service" acceptance or similar fields.

    accepted_if:anotherfield,value,...

    The field under validation must be

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    94,

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    95,

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    96,

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    97,

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    17, or

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    99 if another field under validation is equal to a specified value. This is useful for validating "Terms of Service" acceptance or similar fields.

    active_url

    The field under validation must have a valid A or AAAA record according to the

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    06 PHP function. The hostname of the provided URL is extracted using the

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    07 PHP function before being passed to

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    06.

    after:date

    The field under validation must be a value after a given date. The dates will be passed into the

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    09 PHP function in order to be converted to a valid

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    10 instance:

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    9

    Instead of passing a date string to be evaluated by

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    09, you may specify another field to compare against the date:

    
    $request->validate([
    
        'title' => 'required|unique:posts|max:255',
    
        'author.name' => 'required',
    
        'author.description' => 'required',
    
    ]);
    
    

    0

    after_or_equal:date

    The field under validation must be a value after or equal to the given date. For more information, see the rule.

    alpha

    The field under validation must be entirely Unicode alphabetic characters contained in

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    12 and

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    13.

    To restrict this validation rule to characters in the ASCII range (

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    14 and

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    15), you may provide the

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    16 option to the validation rule:

    
    $request->validate([
    
        'title' => 'required|unique:posts|max:255',
    
        'author.name' => 'required',
    
        'author.description' => 'required',
    
    ]);
    
    

    1

    alpha_dash

    The field under validation must be entirely Unicode alpha-numeric characters contained in

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    12,

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    13,

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    19, as well as ASCII dashes (

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    1. and ASCII underscores (

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    21).

    To restrict this validation rule to characters in the ASCII range (

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    14 and

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    15), you may provide the

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    16 option to the validation rule:

    
    $request->validate([
    
        'title' => 'required|unique:posts|max:255',
    
        'author.name' => 'required',
    
        'author.description' => 'required',
    
    ]);
    
    

    2

    alpha_num

    The field under validation must be entirely Unicode alpha-numeric characters contained in

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    12,

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    13, and

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    19.

    To restrict this validation rule to characters in the ASCII range (

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    14 and

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    15), you may provide the

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    16 option to the validation rule:

    
    $request->validate([
    
        'title' => 'required|unique:posts|max:255',
    
        'author.name' => 'required',
    
        'author.description' => 'required',
    
    ]);
    
    

    3

    array

    The field under validation must be a PHP

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    33.

    When additional values are provided to the

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    33 rule, each key in the input array must be present within the list of values provided to the rule. In the following example, the

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    33 key in the input array is invalid since it is not contained in the list of values provided to the

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    33 rule:

    
    $request->validate([
    
        'title' => 'required|unique:posts|max:255',
    
        'author.name' => 'required',
    
        'author.description' => 'required',
    
    ]);
    
    

    4

    In general, you should always specify the array keys that are allowed to be present within your array.

    ascii

    The field under validation must be entirely 7-bit ASCII characters.

    bail

    Stop running validation rules for the field after the first validation failure.

    While the

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    53 rule will only stop validating a specific field when it encounters a validation failure, the

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    07 method will inform the validator that it should stop validating all attributes once a single validation failure has occurred:

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    2

    before:date

    The field under validation must be a value preceding the given date. The dates will be passed into the PHP

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    09 function in order to be converted into a valid

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    10 instance. In addition, like the rule, the name of another field under validation may be supplied as the value of

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    40.

    before_or_equal:date

    The field under validation must be a value preceding or equal to the given date. The dates will be passed into the PHP

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    09 function in order to be converted into a valid

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    10 instance. In addition, like the rule, the name of another field under validation may be supplied as the value of

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    40.

    between:min,max

    The field under validation must have a size between the given min and max (inclusive). Strings, numerics, arrays, and files are evaluated in the same fashion as the rule.

    boolean

    The field under validation must be able to be cast as a boolean. Accepted input are

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    17,

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    15,

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    96,

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    49,

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    97, and

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    51.

    confirmed

    The field under validation must have a matching field of

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    52. For example, if the field under validation is

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    53, a matching

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    54 field must be present in the input.

    current_password

    The field under validation must match the authenticated user's password. You may specify an authentication guard using the rule's first parameter:

    
    $request->validate([
    
        'title' => 'required|unique:posts|max:255',
    
        'author.name' => 'required',
    
        'author.description' => 'required',
    
    ]);
    
    

    6

    date

    The field under validation must be a valid, non-relative date according to the

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    09 PHP function.

    date_equals:date

    The field under validation must be equal to the given date. The dates will be passed into the PHP

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    09 function in order to be converted into a valid

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    10 instance.

    date_format:format,...

    The field under validation must match one of the given formats. You should use either

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    40 or

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    59 when validating a field, not both. This validation rule supports all formats supported by PHP's DateTime class.

    decimal:min,max

    The field under validation must be numeric and must contain the specified number of decimal places:

    
    $request->validate([
    
        'title' => 'required|unique:posts|max:255',
    
        'author.name' => 'required',
    
        'author.description' => 'required',
    
    ]);
    
    

    7

    declined

    The field under validation must be

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    60,

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    61,

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    49,

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    51,

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    15, or

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    65.

    declined_if:anotherfield,value,...

    The field under validation must be

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    60,

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    61,

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    49,

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    51,

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    15, or

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    65 if another field under validation is equal to a specified value.

    different:field

    The field under validation must have a different value than field.

    digits:value

    The integer under validation must have an exact length of value.

    digits_between:min,max

    The integer validation must have a length between the given min and max.

    dimensions

    The file under validation must be an image meeting the dimension constraints as specified by the rule's parameters:

    
    $request->validate([
    
        'title' => 'required|unique:posts|max:255',
    
        'author.name' => 'required',
    
        'author.description' => 'required',
    
    ]);
    
    

    8

    Available constraints are: min_width, max_width, min_height, max_height, width, height, ratio.

    A ratio constraint should be represented as width divided by height. This can be specified either by a fraction like

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    72 or a float like

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    73:

    
    $request->validate([
    
        'title' => 'required|unique:posts|max:255',
    
        'author.name' => 'required',
    
        'author.description' => 'required',
    
    ]);
    
    

    9

    Since this rule requires several arguments, you may use the

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    74 method to fluently construct the rule:

    
    $request->validate([
    
        'title' => 'required|unique:posts|max:255',
    
        'v1\.0' => 'required',
    
    ]);
    
    

    0

    distinct

    When validating arrays, the field under validation must not have any duplicate values:

    
    $request->validate([
    
        'title' => 'required|unique:posts|max:255',
    
        'v1\.0' => 'required',
    
    ]);
    
    

    1

    Distinct uses loose variable comparisons by default. To use strict comparisons, you may add the

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    75 parameter to your validation rule definition:

    
    $request->validate([
    
        'title' => 'required|unique:posts|max:255',
    
        'v1\.0' => 'required',
    
    ]);
    
    

    2

    You may add

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    76 to the validation rule's arguments to make the rule ignore capitalization differences:

    
    $request->validate([
    
        'title' => 'required|unique:posts|max:255',
    
        'v1\.0' => 'required',
    
    ]);
    
    

    3

    doesnt_start_with:foo,bar,...

    The field under validation must not start with one of the given values.

    doesnt_end_with:foo,bar,...

    The field under validation must not end with one of the given values.

    email

    The field under validation must be formatted as an email address. This validation rule utilizes the

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    77 package for validating the email address. By default, the

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    78 validator is applied, but you can apply other validation styles as well:

    
    $request->validate([
    
        'title' => 'required|unique:posts|max:255',
    
        'v1\.0' => 'required',
    
    ]);
    
    

    4

    The example above will apply the

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    78 and

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    80 validations. Here's a full list of validation styles you can apply:

    • $validatedData = $request->validate([

      'title' => ['required', 'unique:posts', 'max:255'],  
      'body' => ['required'],  
      
      ]);

      81:

      $validatedData = $request->validate([

      'title' => ['required', 'unique:posts', 'max:255'],  
      'body' => ['required'],  
      
      ]);

      78
    • $validatedData = $request->validate([

      'title' => ['required', 'unique:posts', 'max:255'],  
      'body' => ['required'],  
      
      ]);

      75:

      $validatedData = $request->validate([

      'title' => ['required', 'unique:posts', 'max:255'],  
      'body' => ['required'],  
      
      ]);

      84
    • $validatedData = $request->validate([

      'title' => ['required', 'unique:posts', 'max:255'],  
      'body' => ['required'],  
      
      ]);

      85:

      $validatedData = $request->validate([

      'title' => ['required', 'unique:posts', 'max:255'],  
      'body' => ['required'],  
      
      ]);

      80
    • $validatedData = $request->validate([

      'title' => ['required', 'unique:posts', 'max:255'],  
      'body' => ['required'],  
      
      ]);

      87:

      $validatedData = $request->validate([

      'title' => ['required', 'unique:posts', 'max:255'],  
      'body' => ['required'],  
      
      ]);

      88
    • $validatedData = $request->validate([

      'title' => ['required', 'unique:posts', 'max:255'],  
      'body' => ['required'],  
      
      ]);

      89:

      $validatedData = $request->validate([

      'title' => ['required', 'unique:posts', 'max:255'],  
      'body' => ['required'],  
      
      ]);

      90
    • $validatedData = $request->validate([

      'title' => ['required', 'unique:posts', 'max:255'],  
      'body' => ['required'],  
      
      ]);

      91:

      $validatedData = $request->validate([

      'title' => ['required', 'unique:posts', 'max:255'],  
      'body' => ['required'],  
      
      ]);

      92

    The

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    89 validator, which uses PHP's

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    94 function, ships with Laravel and was Laravel's default email validation behavior prior to Laravel version 5.8.

    [!WARNING]

    The

    $validatedData = $request->validate([

    'title' => ['required', 'unique:posts', 'max:255'],

    'body' => ['required'],

    ]);

    85 and

    $validatedData = $request->validate([

    'title' => ['required', 'unique:posts', 'max:255'],

    'body' => ['required'],

    ]);

    87 validators require the PHP

    $validatedData = $request->validate([

    'title' => ['required', 'unique:posts', 'max:255'],

    'body' => ['required'],

    ]);

    97 extension.

    ends_with:foo,bar,...

    The field under validation must end with one of the given values.

    enum

    The

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    98 rule is a class based rule that validates whether the field under validation contains a valid enum value. The

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    98 rule accepts the name of the enum as its only constructor argument. When validating primitive values, a backed Enum should be provided to the

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    98 rule:

    
    $request->validate([
    
        'title' => 'required|unique:posts|max:255',
    
        'v1\.0' => 'required',
    
    ]);
    
    

    5

    The

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    98 rule's

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    55 and

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    56 methods may be used to limit which enum cases should be considered valid:

    
    $request->validate([
    
        'title' => 'required|unique:posts|max:255',
    
        'v1\.0' => 'required',
    
    ]);
    
    

    6

    The

    
    $validatedData = $request->validateWithBag('post', [
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    04 method may be used to conditionally modify the

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    98 rule:

    
    $request->validate([
    
        'title' => 'required|unique:posts|max:255',
    
        'v1\.0' => 'required',
    
    ]);
    
    

    7

    exclude

    The field under validation will be excluded from the request data returned by the

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    39 and

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    52 methods.

    exclude_if:anotherfield,value

    The field under validation will be excluded from the request data returned by the

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    39 and

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    52 methods if the anotherfield field is equal to value.

    If complex conditional exclusion logic is required, you may utilize the

    
    $validatedData = $request->validateWithBag('post', [
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    10 method. This method accepts a boolean or a closure. When given a closure, the closure should return

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    17 or

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    15 to indicate if the field under validation should be excluded:

    
    $request->validate([
    
        'title' => 'required|unique:posts|max:255',
    
        'v1\.0' => 'required',
    
    ]);
    
    

    8

    exclude_unless:anotherfield,value

    The field under validation will be excluded from the request data returned by the

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    39 and

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    52 methods unless anotherfield's field is equal to value. If value is

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    82 (

    
    $validatedData = $request->validateWithBag('post', [
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    16), the field under validation will be excluded unless the comparison field is

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    82 or the comparison field is missing from the request data.

    exclude_with:anotherfield

    The field under validation will be excluded from the request data returned by the

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    39 and

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    52 methods if the anotherfield field is present.

    exclude_without:anotherfield

    The field under validation will be excluded from the request data returned by the

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    39 and

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    52 methods if the anotherfield field is not present.

    exists:table,column

    The field under validation must exist in a given database table.

    Basic Usage of Exists Rule

    
    $request->validate([
    
        'title' => 'required|unique:posts|max:255',
    
        'v1\.0' => 'required',
    
    ]);
    
    

    9

    If the

    
    $validatedData = $request->validateWithBag('post', [
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    22 option is not specified, the field name will be used. So, in this case, the rule will validate that the

    
    $validatedData = $request->validateWithBag('post', [
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    23 database table contains a record with a

    
    $validatedData = $request->validateWithBag('post', [
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    24 column value matching the request's

    
    $validatedData = $request->validateWithBag('post', [
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    24 attribute value.

    Specifying a Custom Column Name

    You may explicitly specify the database column name that should be used by the validation rule by placing it after the database table name:

    
    
    
    

    Create Post

    @if ($errors->any())
      @foreach ($errors->all() as $error)
    • {{ $error }}
    • @endforeach
    @endif

    0

    Occasionally, you may need to specify a specific database connection to be used for the

    
    $validatedData = $request->validateWithBag('post', [
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    26 query. You can accomplish this by prepending the connection name to the table name:

    
    
    
    

    Create Post

    @if ($errors->any())
      @foreach ($errors->all() as $error)
    • {{ $error }}
    • @endforeach
    @endif

    1

    Instead of specifying the table name directly, you may specify the Eloquent model which should be used to determine the table name:

    
    
    
    

    Create Post

    @if ($errors->any())
      @foreach ($errors->all() as $error)
    • {{ $error }}
    • @endforeach
    @endif

    2

    If you would like to customize the query executed by the validation rule, you may use the

    
    $validatedData = $request->validateWithBag('post', [
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    27 class to fluently define the rule. In this example, we'll also specify the validation rules as an array instead of using the

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    51 character to delimit them:

    
    
    
    

    Create Post

    @if ($errors->any())
      @foreach ($errors->all() as $error)
    • {{ $error }}
    • @endforeach
    @endif

    3

    You may explicitly specify the database column name that should be used by the

    
    $validatedData = $request->validateWithBag('post', [
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    26 rule generated by the

    
    $validatedData = $request->validateWithBag('post', [
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    30 method by providing the column name as the second argument to the

    
    $validatedData = $request->validateWithBag('post', [
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    26 method:

    
    
    
    

    Create Post

    @if ($errors->any())
      @foreach ($errors->all() as $error)
    • {{ $error }}
    • @endforeach
    @endif

    4

    extensions:foo,bar,...

    The file under validation must have a user-assigned extension corresponding to one of the listed extensions:

    
    
    
    

    Create Post

    @if ($errors->any())
      @foreach ($errors->all() as $error)
    • {{ $error }}
    • @endforeach
    @endif

    5

    [!WARNING] You should never rely on validating a file by its user-assigned extension alone. This rule should typically always be used in combination with the or rules.

    file

    The field under validation must be a successfully uploaded file.

    filled

    The field under validation must not be empty when it is present.

    gt:field

    The field under validation must be greater than the given field or value. The two fields must be of the same type. Strings, numerics, arrays, and files are evaluated using the same conventions as the rule.

    gte:field

    The field under validation must be greater than or equal to the given field or value. The two fields must be of the same type. Strings, numerics, arrays, and files are evaluated using the same conventions as the rule.

    hex_color

    The field under validation must contain a valid color value in hexadecimal format.

    image

    The file under validation must be an image (jpg, jpeg, png, bmp, gif, svg, or webp).

    in:foo,bar,...

    The field under validation must be included in the given list of values. Since this rule often requires you to

    
    $validatedData = $request->validateWithBag('post', [
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    36 an array, the

    
    $validatedData = $request->validateWithBag('post', [
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    37 method may be used to fluently construct the rule:

    
    
    
    

    Create Post

    @if ($errors->any())
      @foreach ($errors->all() as $error)
    • {{ $error }}
    • @endforeach
    @endif

    6

    When the

    
    $validatedData = $request->validateWithBag('post', [
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    38 rule is combined with the

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    33 rule, each value in the input array must be present within the list of values provided to the

    
    $validatedData = $request->validateWithBag('post', [
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    38 rule. In the following example, the

    
    $validatedData = $request->validateWithBag('post', [
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    41 airport code in the input array is invalid since it is not contained in the list of airports provided to the

    
    $validatedData = $request->validateWithBag('post', [
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    38 rule:

    
    
    
    

    Create Post

    @if ($errors->any())
      @foreach ($errors->all() as $error)
    • {{ $error }}
    • @endforeach
    @endif

    7

    in_array:anotherfield.*

    The field under validation must exist in anotherfield's values.

    integer

    The field under validation must be an integer.

    [!WARNING]

    This validation rule does not verify that the input is of the "integer" variable type, only that the input is of a type accepted by PHP's

    $validatedData = $request->validateWithBag('post', [

    'title' => ['required', 'unique:posts', 'max:255'],

    'body' => ['required'],

    ]);

    43 rule. If you need to validate the input as being a number please use this rule in combination with .

    ip

    The field under validation must be an IP address.

    ipv4

    The field under validation must be an IPv4 address.

    ipv6

    The field under validation must be an IPv6 address.

    json

    The field under validation must be a valid JSON string.

    lt:field

    The field under validation must be less than the given field. The two fields must be of the same type. Strings, numerics, arrays, and files are evaluated using the same conventions as the rule.

    lte:field

    The field under validation must be less than or equal to the given field. The two fields must be of the same type. Strings, numerics, arrays, and files are evaluated using the same conventions as the rule.

    lowercase

    The field under validation must be lowercase.

    list

    The field under validation must be an array that is a list. An array is considered a list if its keys consist of consecutive numbers from 0 to

    
    $validatedData = $request->validateWithBag('post', [
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    47.

    mac_address

    The field under validation must be a MAC address.

    max:value

    The field under validation must be less than or equal to a maximum value. Strings, numerics, arrays, and files are evaluated in the same fashion as the rule.

    max_digits:value

    The integer under validation must have a maximum length of value.

    mimetypes:text/plain,...

    The file under validation must match one of the given MIME types:

    
    
    
    

    Create Post

    @if ($errors->any())
      @foreach ($errors->all() as $error)
    • {{ $error }}
    • @endforeach
    @endif

    8

    To determine the MIME type of the uploaded file, the file's contents will be read and the framework will attempt to guess the MIME type, which may be different from the client's provided MIME type.

    mimes:foo,bar,...

    The file under validation must have a MIME type corresponding to one of the listed extensions:

    
    
    
    

    Create Post

    @if ($errors->any())
      @foreach ($errors->all() as $error)
    • {{ $error }}
    • @endforeach
    @endif

    9

    Even though you only need to specify the extensions, this rule actually validates the MIME type of the file by reading the file's contents and guessing its MIME type. A full listing of MIME types and their corresponding extensions may be found at the following location:

    https://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types

    MIME Types and Extensions

    This validation rule does not verify agreement between the MIME type and the extension the user assigned to the file. For example, the

    
    $validatedData = $request->validateWithBag('post', [
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    49 validation rule would consider a file containing valid PNG content to be a valid PNG image, even if the file is named

    
    $validatedData = $request->validateWithBag('post', [
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    50. If you would like to validate the user-assigned extension of the file, you may use the rule.

    min:value

    The field under validation must have a minimum value. Strings, numerics, arrays, and files are evaluated in the same fashion as the rule.

    min_digits:value

    The integer under validation must have a minimum length of value.

    multiple_of:value

    The field under validation must be a multiple of value.

    missing

    The field under validation must not be present in the input data.

    missing_if:anotherfield,value,...

    The field under validation must not be present if the anotherfield field is equal to any value.

    missing_unless:anotherfield,value

    The field under validation must not be present unless the anotherfield field is equal to any value.

    missing_with:foo,bar,...

    The field under validation must not be present only if any of the other specified fields are present.

    missing_with_all:foo,bar,...

    The field under validation must not be present only if all of the other specified fields are present.

    not_in:foo,bar,...

    The field under validation must not be included in the given list of values. The

    
    $validatedData = $request->validateWithBag('post', [
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    53 method may be used to fluently construct the rule:

    
    
    
    
    
    
    
    @error('title')
    
        
    {{ $message }}
    @enderror

    0

    not_regex:pattern

    The field under validation must not match the given regular expression.

    Internally, this rule uses the PHP

    
    $validatedData = $request->validateWithBag('post', [
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    54 function. The pattern specified should obey the same formatting required by

    
    $validatedData = $request->validateWithBag('post', [
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    54 and thus also include valid delimiters. For example:

    
    $validatedData = $request->validateWithBag('post', [
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    56.

    [!WARNING]

    When using the

    $validatedData = $request->validateWithBag('post', [

    'title' => ['required', 'unique:posts', 'max:255'],

    'body' => ['required'],

    ]);

    57 /

    $validatedData = $request->validateWithBag('post', [

    'title' => ['required', 'unique:posts', 'max:255'],

    'body' => ['required'],

    ]);

    58 patterns, it may be necessary to specify your validation rules using an array instead of using

    namespace App\Http\Controllers;

    use Illuminate\Http\RedirectResponse;

    use Illuminate\Http\Request;

    use Illuminate\View\View;

    class PostController extends Controller

    {

    / Show the form to create a new blog post. /

    public function create(): View

    {

    return view('post.create');

    }

    /
    Store a new blog post. /

    public function store(Request $request): RedirectResponse

    {

    // Validate and store the blog post...

    $post = / ... */

    return to_route('post.show', ['post' => $post->id]);

    }

    }

    51 delimiters, especially if the regular expression contains a

    namespace App\Http\Controllers;

    use Illuminate\Http\RedirectResponse;

    use Illuminate\Http\Request;

    use Illuminate\View\View;

    class PostController extends Controller

    {

    / Show the form to create a new blog post. /

    public function create(): View

    {

    return view('post.create');

    }

    /
    Store a new blog post. /

    public function store(Request $request): RedirectResponse

    {

    // Validate and store the blog post...

    $post = / ... */

    return to_route('post.show', ['post' => $post->id]);

    }

    }

    51 character.

    nullable

    The field under validation may be

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    82.

    numeric

    The field under validation must be numeric.

    present

    The field under validation must exist in the input data.

    present_if:anotherfield,value,...

    The field under validation must be present if the anotherfield field is equal to any value.

    present_unless:anotherfield,value

    The field under validation must be present unless the anotherfield field is equal to any value.

    present_with:foo,bar,...

    The field under validation must be present only if any of the other specified fields are present.

    present_with_all:foo,bar,...

    The field under validation must be present only if all of the other specified fields are present.

    prohibited

    The field under validation must be missing or empty. A field is "empty" if it meets one of the following criteria:

    • The value is ` /
      • Show the form to create a new blog post.
         /  
        public function create(): View  
        {  
            return view('post.create');  
        }  
        /*  
      • Store a new blog post. / public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /* ... */ return to_route('post.show', ['post' => $post->id]); } } ` 82.
    • The value is an empty string.
    • The value is an empty array or empty

      $validatedData = $request->validateWithBag('post', [

      'title' => ['required', 'unique:posts', 'max:255'],  
      'body' => ['required'],  
      
      ]);

      63 object.
    • The value is an uploaded file with an empty path.

    prohibited_if:anotherfield,value,...

    The field under validation must be missing or empty if the anotherfield field is equal to any value. A field is "empty" if it meets one of the following criteria:

    • The value is ` /
      • Show the form to create a new blog post.
         /  
        public function create(): View  
        {  
            return view('post.create');  
        }  
        /*  
      • Store a new blog post. / public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /* ... */ return to_route('post.show', ['post' => $post->id]); } } ` 82.
    • The value is an empty string.
    • The value is an empty array or empty

      $validatedData = $request->validateWithBag('post', [

      'title' => ['required', 'unique:posts', 'max:255'],  
      'body' => ['required'],  
      
      ]);

      63 object.
    • The value is an uploaded file with an empty path.

    If complex conditional prohibition logic is required, you may utilize the

    
    $validatedData = $request->validateWithBag('post', [
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    66 method. This method accepts a boolean or a closure. When given a closure, the closure should return

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    17 or

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    15 to indicate if the field under validation should be prohibited:

    
    
    
    
    
    
    
    @error('title')
    
        
    {{ $message }}
    @enderror

    1

    prohibited_unless:anotherfield,value,...

    The field under validation must be missing or empty unless the anotherfield field is equal to any value. A field is "empty" if it meets one of the following criteria:

    • The value is ` /
      • Show the form to create a new blog post.
         /  
        public function create(): View  
        {  
            return view('post.create');  
        }  
        /*  
      • Store a new blog post. / public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /* ... */ return to_route('post.show', ['post' => $post->id]); } } ` 82.
    • The value is an empty string.
    • The value is an empty array or empty

      $validatedData = $request->validateWithBag('post', [

      'title' => ['required', 'unique:posts', 'max:255'],  
      'body' => ['required'],  
      
      ]);

      63 object.
    • The value is an uploaded file with an empty path.

    prohibits:anotherfield,...

    If the field under validation is not missing or empty, all fields in anotherfield must be missing or empty. A field is "empty" if it meets one of the following criteria:

    • The value is ` /
      • Show the form to create a new blog post.
         /  
        public function create(): View  
        {  
            return view('post.create');  
        }  
        /*  
      • Store a new blog post. / public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /* ... */ return to_route('post.show', ['post' => $post->id]); } } ` 82.
    • The value is an empty string.
    • The value is an empty array or empty

      $validatedData = $request->validateWithBag('post', [

      'title' => ['required', 'unique:posts', 'max:255'],  
      'body' => ['required'],  
      
      ]);

      63 object.
    • The value is an uploaded file with an empty path.

    regex:pattern

    The field under validation must match the given regular expression.

    Internally, this rule uses the PHP

    
    $validatedData = $request->validateWithBag('post', [
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    54 function. The pattern specified should obey the same formatting required by

    
    $validatedData = $request->validateWithBag('post', [
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    54 and thus also include valid delimiters. For example:

    
    $validatedData = $request->validateWithBag('post', [
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    75.

    [!WARNING]

    When using the

    $validatedData = $request->validateWithBag('post', [

    'title' => ['required', 'unique:posts', 'max:255'],

    'body' => ['required'],

    ]);

    57 /

    $validatedData = $request->validateWithBag('post', [

    'title' => ['required', 'unique:posts', 'max:255'],

    'body' => ['required'],

    ]);

    58 patterns, it may be necessary to specify rules in an array instead of using

    namespace App\Http\Controllers;

    use Illuminate\Http\RedirectResponse;

    use Illuminate\Http\Request;

    use Illuminate\View\View;

    class PostController extends Controller

    {

    / Show the form to create a new blog post. /

    public function create(): View

    {

    return view('post.create');

    }

    /
    Store a new blog post. /

    public function store(Request $request): RedirectResponse

    {

    // Validate and store the blog post...

    $post = / ... */

    return to_route('post.show', ['post' => $post->id]);

    }

    }

    51 delimiters, especially if the regular expression contains a

    namespace App\Http\Controllers;

    use Illuminate\Http\RedirectResponse;

    use Illuminate\Http\Request;

    use Illuminate\View\View;

    class PostController extends Controller

    {

    / Show the form to create a new blog post. /

    public function create(): View

    {

    return view('post.create');

    }

    /
    Store a new blog post. /

    public function store(Request $request): RedirectResponse

    {

    // Validate and store the blog post...

    $post = / ... */

    return to_route('post.show', ['post' => $post->id]);

    }

    }

    51 character.

    required

    The field under validation must be present in the input data and not empty. A field is "empty" if it meets one of the following criteria:

    • The value is ` /
      • Show the form to create a new blog post.
         /  
        public function create(): View  
        {  
            return view('post.create');  
        }  
        /*  
      • Store a new blog post. / public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /* ... */ return to_route('post.show', ['post' => $post->id]); } } ` 82.
    • The value is an empty string.
    • The value is an empty array or empty

      $validatedData = $request->validateWithBag('post', [

      'title' => ['required', 'unique:posts', 'max:255'],  
      'body' => ['required'],  
      
      ]);

      63 object.
    • The value is an uploaded file with no path.

    required_if:anotherfield,value,...

    The field under validation must be present and not empty if the anotherfield field is equal to any value.

    If you would like to construct a more complex condition for the

    
    $validatedData = $request->validateWithBag('post', [
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    82 rule, you may use the

    
    $validatedData = $request->validateWithBag('post', [
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    83 method. This method accepts a boolean or a closure. When passed a closure, the closure should return

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    17 or

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    15 to indicate if the field under validation is required:

    
    
    
    
    
    
    
    @error('title')
    
        
    {{ $message }}
    @enderror

    2

    required_if_accepted:anotherfield,...

    The field under validation must be present and not empty if the anotherfield field is equal to

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    94,

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    95,

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    96,

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    97,

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    17, or

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    99.

    required_unless:anotherfield,value,...

    The field under validation must be present and not empty unless the anotherfield field is equal to any value. This also means anotherfield must be present in the request data unless value is

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    82. If value is

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    82 (

    
    $validatedData = $request->validateWithBag('post', [
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    94), the field under validation will be required unless the comparison field is

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    82 or the comparison field is missing from the request data.

    required_with:foo,bar,...

    The field under validation must be present and not empty only if any of the other specified fields are present and not empty.

    required_with_all:foo,bar,...

    The field under validation must be present and not empty only if all of the other specified fields are present and not empty.

    required_without:foo,bar,...

    The field under validation must be present and not empty only when any of the other specified fields are empty or not present.

    required_without_all:foo,bar,...

    The field under validation must be present and not empty only when all of the other specified fields are empty or not present.

    required_array_keys:foo,bar,...

    The field under validation must be an array and must contain at least the specified keys.

    same:field

    The given field must match the field under validation.

    size:value

    The field under validation must have a size matching the given value. For string data, value corresponds to the number of characters. For numeric data, value corresponds to a given integer value (the attribute must also have the

    
    $validatedData = $request->validateWithBag('post', [
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    44 or

    
    $validatedData = $request->validateWithBag('post', [
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    97 rule). For an array, size corresponds to the

    
    $validatedData = $request->validateWithBag('post', [
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    98 of the array. For files, size corresponds to the file size in kilobytes. Let's look at some examples:

    
    
    
    
    
    
    
    @error('title')
    
        
    {{ $message }}
    @enderror

    3

    starts_with:foo,bar,...

    The field under validation must start with one of the given values.

    string

    The field under validation must be a string. If you would like to allow the field to also be

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    82, you should assign the

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    85 rule to the field.

    timezone

    The field under validation must be a valid timezone identifier according to the

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    01 method.

    The arguments accepted by the

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    01 method may also be provided to this validation rule:

    
    
    
    
    
    
    
    @error('title')
    
        
    {{ $message }}
    @enderror

    4

    unique:table,column

    The field under validation must not exist within the given database table.

    Specifying a Custom Table / Column Name:

    Instead of specifying the table name directly, you may specify the Eloquent model which should be used to determine the table name:

    
    
    
    
    
    
    
    @error('title')
    
        
    {{ $message }}
    @enderror

    5

    The

    
    $validatedData = $request->validateWithBag('post', [
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    22 option may be used to specify the field's corresponding database column. If the

    
    $validatedData = $request->validateWithBag('post', [
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    22 option is not specified, the name of the field under validation will be used.

    
    
    
    
    
    
    
    @error('title')
    
        
    {{ $message }}
    @enderror

    6

    Specifying a Custom Database Connection

    Occasionally, you may need to set a custom connection for database queries made by the Validator. To accomplish this, you may prepend the connection name to the table name:

    
    
    
    
    
    
    
    @error('title')
    
        
    {{ $message }}
    @enderror

    7

    Forcing a Unique Rule to Ignore a Given ID:

    Sometimes, you may wish to ignore a given ID during unique validation. For example, consider an "update profile" screen that includes the user's name, email address, and location. You will probably want to verify that the email address is unique. However, if the user only changes the name field and not the email field, you do not want a validation error to be thrown because the user is already the owner of the email address in question.

    To instruct the validator to ignore the user's ID, we'll use the

    
    $validatedData = $request->validateWithBag('post', [
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    27 class to fluently define the rule. In this example, we'll also specify the validation rules as an array instead of using the

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    51 character to delimit the rules:

    
    
    
    
    
    
    
    @error('title')
    
        
    {{ $message }}
    @enderror

    8

    [!WARNING]

    You should never pass any user controlled request input into the

    $request->validate([

    'title' => 'bail|required|unique:posts|max:255',

    'body' => 'required',

    ]);

    07 method. Instead, you should only pass a system generated unique ID such as an auto-incrementing ID or UUID from an Eloquent model instance. Otherwise, your application will be vulnerable to an SQL injection attack.

    Instead of passing the model key's value to the

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    07 method, you may also pass the entire model instance. Laravel will automatically extract the key from the model:

    
    
    
    
    
    
    
    @error('title')
    
        
    {{ $message }}
    @enderror

    9

    If your table uses a primary key column name other than

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    09, you may specify the name of the column when calling the

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    07 method:

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    00

    By default, the

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    54 rule will check the uniqueness of the column matching the name of the attribute being validated. However, you may pass a different column name as the second argument to the

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    54 method:

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    01

    Adding Additional Where Clauses:

    You may specify additional query conditions by customizing the query using the

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    13 method. For example, let's add a query condition that scopes the query to only search records that have an

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    14 column value of

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    96:

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    02

    uppercase

    The field under validation must be uppercase.

    url

    The field under validation must be a valid URL.

    If you would like to specify the URL protocols that should be considered valid, you may pass the protocols as validation rule parameters:

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    03

    ulid

    The field under validation must be a valid Universally Unique Lexicographically Sortable Identifier (ULID).

    uuid

    The field under validation must be a valid RFC 4122 (version 1, 3, 4, or 5) universally unique identifier (UUID).

    Conditionally Adding Rules

    Skipping Validation When Fields Have Certain Values

    You may occasionally wish to not validate a given field if another field has a given value. You may accomplish this using the

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    16 validation rule. In this example, the

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    17 and

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    18 fields will not be validated if the

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    19 field has a value of

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    15:

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    04

    Alternatively, you may use the

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    21 rule to not validate a given field unless another field has a given value:

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    05

    Validating When Present

    In some situations, you may wish to run validation checks against a field only if that field is present in the data being validated. To quickly accomplish this, add the

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    22 rule to your rule list:

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    06

    In the example above, the

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    23 field will only be validated if it is present in the

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    24 array.

    [!NOTE] If you are attempting to validate a field that should always be present but may be empty, check out .

    Complex Conditional Validation

    Sometimes you may wish to add validation rules based on more complex conditional logic. For example, you may wish to require a given field only if another field has a greater value than 100. Or, you may need two fields to have a given value only when another field is present. Adding these validation rules doesn't have to be a pain. First, create a

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    26 instance with your static rules that never change:

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    07

    Let's assume our web application is for game collectors. If a game collector registers with our application and they own more than 100 games, we want them to explain why they own so many games. For example, perhaps they run a game resale shop, or maybe they just enjoy collecting games. To conditionally add this requirement, we can use the

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    22 method on the

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    26 instance.

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    08

    The first argument passed to the

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    22 method is the name of the field we are conditionally validating. The second argument is a list of the rules we want to add. If the closure passed as the third argument returns

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    17, the rules will be added. This method makes it a breeze to build complex conditional validations. You may even add conditional validations for several fields at once:

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    09

    [!NOTE]

    The

    $request->validate([

    'title' => 'bail|required|unique:posts|max:255',

    'body' => 'required',

    ]);

    30 parameter passed to your closure will be an instance of

    $request->validate([

    'title' => 'bail|required|unique:posts|max:255',

    'body' => 'required',

    ]);

    31 and may be used to access your input and files under validation.

    Complex Conditional Array Validation

    Sometimes you may want to validate a field based on another field in the same nested array whose index you do not know. In these situations, you may allow your closure to receive a second argument which will be the current individual item in the array being validated:

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    10

    Like the

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    30 parameter passed to the closure, the

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    33 parameter is an instance of

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    31 when the attribute data is an array; otherwise, it is a string.

    Validating Arrays

    As discussed in the , the

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    33 rule accepts a list of allowed array keys. If any additional keys are present within the array, validation will fail:

    
    $request->validate([
    
        'title' => 'required|unique:posts|max:255',
    
        'author.name' => 'required',
    
        'author.description' => 'required',
    
    ]);
    
    

    4

    In general, you should always specify the array keys that are allowed to be present within your array. Otherwise, the validator's

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    39 and

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    52 methods will return all of the validated data, including the array and all of its keys, even if those keys were not validated by other nested array validation rules.

    Validating Nested Array Input

    Validating nested array based form input fields doesn't have to be a pain. You may use "dot notation" to validate attributes within an array. For example, if the incoming HTTP request contains a

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    39 field, you may validate it like so:

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    12

    You may also validate each element of an array. For example, to validate that each email in a given array input field is unique, you may do the following:

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    13

    Likewise, you may use the

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    68 character when specifying , making it a breeze to use a single validation message for array based fields:

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    14

    Accessing Nested Array Data

    Sometimes you may need to access the value for a given nested array element when assigning validation rules to the attribute. You may accomplish this using the

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    41 method. The

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    42 method accepts a closure that will be invoked for each iteration of the array attribute under validation and will receive the attribute's value and explicit, fully-expanded attribute name. The closure should return an array of rules to assign to the array element:

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    15

    Error Message Indexes and Positions

    When validating arrays, you may want to reference the index or position of a particular item that failed validation within the error message displayed by your application. To accomplish this, you may include the

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    43 (starts from

    
    $validatedData = $request->validate([
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    1. and

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    45 (starts from

    
    /**
    
    
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // The blog post is valid... return redirect('/posts'); }

    1. placeholders within your :

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    16

    Given the example above, validation will fail and the user will be presented with the following error of "Please describe photo

    2."

    If necessary, you may reference more deeply nested indexes and positions via

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    47,

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    48,

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    49,

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    50, etc.

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    17

    Validating Files

    Laravel provides a variety of validation rules that may be used to validate uploaded files, such as

    
    $validatedData = $request->validateWithBag('post', [
    
        'title' => ['required', 'unique:posts', 'max:255'],
    
        'body' => ['required'],
    
    ]);
    
    

    32,

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    52,

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    53, and

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    56. While you are free to specify these rules individually when validating files, Laravel also offers a fluent file validation rule builder that you may find convenient:

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    18

    If your application accepts images uploaded by your users, you may use the

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    55 rule's

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    52 constructor method to indicate that the uploaded file should be an image. In addition, the

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    57 rule may be used to limit the dimensions of the image:

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    19

    [!NOTE] More information regarding validating image dimensions may be found in the .

    File Sizes

    For convenience, minimum and maximum file sizes may be specified as a string with a suffix indicating the file size units. The

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    58,

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    59,

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    60, and

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    61 suffixes are supported:

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    20

    File Types

    Even though you only need to specify the extensions when invoking the

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    62 method, this method actually validates the MIME type of the file by reading the file's contents and guessing its MIME type. A full listing of MIME types and their corresponding extensions may be found at the following location:

    https://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types

    Validating Passwords

    To ensure that passwords have an adequate level of complexity, you may use Laravel's

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    63 rule object:

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    21

    The

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    63 rule object allows you to easily customize the password complexity requirements for your application, such as specifying that passwords require at least one letter, number, symbol, or characters with mixed casing:

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    22

    In addition, you may ensure that a password has not been compromised in a public password data breach leak using the

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    65 method:

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    23

    Internally, the

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    63 rule object uses the k-Anonymity model to determine if a password has been leaked via the haveibeenpwned.com service without sacrificing the user's privacy or security.

    By default, if a password appears at least once in a data leak, it will be considered compromised. You can customize this threshold using the first argument of the

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    65 method:

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    24

    Of course, you may chain all the methods in the examples above:

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    25

    Defining Default Password Rules

    You may find it convenient to specify the default validation rules for passwords in a single location of your application. You can easily accomplish this using the

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    68 method, which accepts a closure. The closure given to the

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    69 method should return the default configuration of the Password rule. Typically, the

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    69 rule should be called within the

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    71 method of one of your application's service providers:

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    26

    Then, when you would like to apply the default rules to a particular password undergoing validation, you may invoke the

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    69 method with no arguments:

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    27

    Occasionally, you may want to attach additional validation rules to your default password validation rules. You may use the

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    97 method to accomplish this:

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    28

    Custom Validation Rules

    Using Rule Objects

    Laravel provides a variety of helpful validation rules; however, you may wish to specify some of your own. One method of registering custom validation rules is using rule objects. To generate a new rule object, you may use the

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    74 Artisan command. Let's use this command to generate a rule that verifies a string is uppercase. Laravel will place the new rule in the

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    75 directory. If this directory does not exist, Laravel will create it when you execute the Artisan command to create your rule:

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    29

    Once the rule has been created, we are ready to define its behavior. A rule object contains a single method:

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    39. This method receives the attribute name, its value, and a callback that should be invoked on failure with the validation error message:

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    30

    Once the rule has been defined, you may attach it to a validator by passing an instance of the rule object with your other validation rules:

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    31

    Translating Validation Messages

    Instead of providing a literal error message to the

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    77 closure, you may also provide a translation string key and instruct Laravel to translate the error message:

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    32

    If necessary, you may provide placeholder replacements and the preferred language as the first and second arguments to the

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    78 method:

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    33

    Accessing Additional Data

    If your custom validation rule class needs to access all of the other data undergoing validation, your rule class may implement the

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    79 interface. This interface requires your class to define a

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    80 method. This method will automatically be invoked by Laravel (before validation proceeds) with all of the data under validation:

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    34

    Or, if your validation rule requires access to the validator instance performing the validation, you may implement the

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    81 interface:

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    35

    Using Closures

    If you only need the functionality of a custom rule once throughout your application, you may use a closure instead of a rule object. The closure receives the attribute's name, the attribute's value, and a

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    77 callback that should be called if validation fails:

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    36

    Implicit Rules

    By default, when an attribute being validated is not present or contains an empty string, normal validation rules, including custom rules, are not run. For example, the rule will not be run against an empty string:

    
    
  • Show the form to create a new blog post.
  • */ public function create(): View { return view('post.create'); } /**
    • Store a new blog post.
    */ public function store(Request $request): RedirectResponse { // Validate and store the blog post... $post = /** ... */ return to_route('post.show', ['post' => $post->id]); } }

    37

    For a custom rule to run even when an attribute is empty, the rule must imply that the attribute is required. To quickly generate a new implicit rule object, you may use the

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    74 Artisan command with the

    
    $request->validate([
    
        'title' => 'bail|required|unique:posts|max:255',
    
        'body' => 'required',
    
    ]);
    
    

    85 option: