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.
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:
use App\Http\Controllers\PostController;Route::get('/post/create', [PostController::class, 'create']);Route::post('/post', [PostController::class, 'store']);
Then write the controller:
<?phpnamespace App\Http\Controllers;use Illuminate\Http\RedirectResponse;use Illuminate\Http\Request;use Illuminate\View\View;class PostController extends Controller{ public function create(): View { return view('post.create'); } public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'title' => 'required|string|max:255', 'body' => 'required|string', 'email' => 'required|email', ]); // Save the post using validated data... return redirect('/posts'); }}
Pass an array to 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:
The validate method returns only the data that passed validation. Use this array directly — it contains nothing more than what you explicitly declared:
When validation fails, Laravel makes an $errors variable available in every view. The ShareErrorsFromSession middleware in the web middleware group handles this automatically.
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.
This creates app/Http/Requests/StorePostRequest.php:
<?phpnamespace App\Http\Requests;use Illuminate\Foundation\Http\FormRequest;class StorePostRequest extends FormRequest{ /** * Determine if the user is authorized to make this request. */ public function authorize(): bool { return true; } /** * Get the validation rules that apply to the request. * * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string> */ public function rules(): array { return [ 'title' => 'required|string|max:255', 'body' => 'required|string', 'email' => 'required|email|unique:posts', ]; }}
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.
If authorize returns false, Laravel returns a 403 Forbidden response and the controller method is never called.
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:
use App\Http\Requests\StorePostRequest;class PostController extends Controller{ public function store(StorePostRequest $request): RedirectResponse { // Execution only reaches here if validation passed $validated = $request->validated(); Post::create($validated); return redirect('/posts'); }}
Call $request->validated() to retrieve only the data that passed validation.
Here is the full flow — from displaying a form to validating and saving the data.
1
Define routes
use App\Http\Controllers\PostController;Route::get('/posts/create', [PostController::class, 'create']);Route::post('/posts', [PostController::class, 'store']);
2
Create a form request
php artisan make:request StorePostRequest
public function rules(): array{ return [ 'title' => 'required|string|max:255', 'body' => 'required|string', ];}public function authorize(): bool{ return true;}
3
Implement the controller
use App\Http\Requests\StorePostRequest;use App\Models\Post;class PostController extends Controller{ public function create(): View { return view('posts.create'); } public function store(StorePostRequest $request): RedirectResponse { Post::create($request->validated()); return redirect('/posts'); }}