User-Friendly Form Validation in Laravel
Separate validated data from raw input and show clear error messages.
You are reading a translated version.
Validate before saving
Forms are an entry point for data. In a Laravel controller, validation checks that the title and excerpt satisfy the rules before they are written to the database.
$data = $request->validate([
'title' => ['required', 'string', 'max:200'],
'excerpt' => ['required', 'string', 'max:500'],
]);
When validation fails during a standard web request, Laravel redirects the user back with errors and the previous input.
Preserve the user's work
Use old('title') as the input value so the user does not have to retype it. Display errors near their corresponding fields and provide clear labels.
Limit the data you save
Use the result of $request->validate() as the basis for persistence. Avoid sending all raw input to a model, particularly when it contains internal fields such as article ownership or access permissions.
Validation is not authorization
Correctly formatted data does not necessarily mean the user may change it. Check permissions through middleware or policies before processing changes.
