13 Laravel 11 Basics: Validation

Learn the essentials of Laravel 11 validation and enhance your development skills with practical examples and best practices.
Defining the Routes
First, let's assume we have the following routes defined in our routes/web.php
file:
use App\Http\Controllers\PostController;
Route::get('/post/create', [PostController::class, 'create']);
Route::post('/post', [PostController::class, 'store']);
The GET
route displays a form for the user to create a new blog post, while the POST
route stores the new blog post in the database.
Creating the Controller
Next, let's look at a simple controller that handles incoming requests to these routes. We'll leave the store
method empty for now:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Http\RedirectResponse;
class PostController extends Controller
{
public function create()
{
return view('post.create');
}
public function store(Request $request): RedirectResponse
{
// Validation logic will go here
}
}
Writing the Validation Logic
Now we are ready to fill in our store
method with the logic to validate the new blog post. To do this, we will use the validate
method provided by the Illuminate\Http\Request
object:
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 validate
method. If validation fails, an Illuminate\Validation\ValidationException
will be thrown, and the proper error response will be automatically sent back to the user.
Displaying the Validation Errors
So, what if the incoming request fields do not pass the given validation rules? Laravel will automatically redirect the user back to their previous location and all validation errors and request input will be flashed to the session. An $errors
variable is shared with all views, allowing you to display error messages conveniently:
<!-- resources/views/post/create.blade.php -->
@if ($errors->any())
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
@endif
Form Request Validation
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:
php artisan make:request StorePostRequest
The generated form request class will be placed in the app/Http/Requests
directory. Each form request has two methods: authorize
and rules
.
Customizing the Error Messages
Laravel's built-in validation rules each have an error message located in your application's lang/en/validation.php
file. You can change or modify these messages based on the needs of your application. For example:
public function messages(): array
{
return [
'title.required' => 'A title is required',
'body.required' => 'A message is required',
];
}
Specifying Custom Attributes
To replace the :attribute
placeholder in validation messages with custom attribute names, you may specify custom names in the attributes
method:
public function attributes(): array
{
return [
'email' => 'email address',
];
}
Conclusion
Laravel 11 provides powerful and flexible validation features that help ensure the integrity of your application's data. By mastering these validation techniques, you can build robust and reliable web applications.
For more detailed information, refer to the official Laravel 11 Validation Documentation.
Discuss Your Project with Us
We're here to help with your web development needs. Schedule a call to discuss your project and how we can assist you.
Let's find the best solutions for your needs.