Documentation Index
Fetch the complete documentation index at: https://kawax.biz/llms.txt
Use this file to discover all available pages before exploring further.
What is validation?
Validation is the process of verifying that data submitted by the user meets the format and rules your application expects. Laravel provides a simple, expressive validation system built into the request object, with no third-party packages required.Validating in a controller
Using $request->validate()
The most straightforward way to validate incoming data is to call $request->validate() inside a controller method. If validation fails, Laravel automatically redirects the user back to the previous page and stores the error messages in the session.
First, define your routes:
validate where each key is a field name and each value is the validation rules for that field. You can express rules either as a pipe-delimited string or as an array:
Common validation rules
| Rule | Description |
|---|---|
required | The field must be present and non-empty |
string | The field must be a string |
email | The field must be a valid email address |
min:value | The string length or numeric value must be at least the given value |
max:value | The string length or numeric value must not exceed the given value |
unique:table | The value must be unique in the given database table |
nullable | The field may be null |
integer | The field must be an integer |
boolean | The field must be true, false, 1, or 0 |
date | The field must be a valid date |
confirmed | The field must match a corresponding field_confirmation field |
For the full list of available validation rules, see the official documentation.
Using validated data
Thevalidate method returns only the data that passed validation. Use this array directly — it contains nothing more than what you explicitly declared:
Displaying errors in Blade
When validation fails, Laravel makes an$errors variable available in every view. The ShareErrorsFromSession middleware in the web middleware group handles this automatically.
Display all errors
Display errors per field
Use the@error directive to show an inline error message for a specific field:
Form request classes
When to use them
When validation logic becomes complex, extract it into a dedicated “form request” class. A form request encapsulates both the validation rules and the authorization check for a request.Creating a form request
Use themake:request Artisan command:
app/Http/Requests/StorePostRequest.php:
The rules() method
Return your validation rules as an array, using the same format as $request->validate().
The authorize() method
Return true to allow the request, or false to automatically return a 403 response. You can check the authenticated user here. For tutorials, returning true is fine.
Using the form request in a controller
Type-hint the form request class on your controller method. Laravel validates and authorizes the request before your method is called, so you can skip writing any validation code inside the method:$request->validated() to retrieve only the data that passed validation.
Practical example: a post creation form
Here is the full flow — from displaying a form to validating and saving the data.Always include the
@csrf directive in your Blade forms. Without it, Laravel returns a 419 error.Next steps
Controllers
Review how to organize request handling logic in controller classes.