Imagine checking in at a friendly hotel. Before the receptionist hands you a room key, they politely glance at your registration card to make sure you wrote your name and a valid email address. If you accidentally left a box blank, they gently hand the pen back and point to the missing line.
In Laravel, Form Validation works just like that receptionist. It checks the information a visitor types into a form before your application saves it to the database.
Why Validation Matters for Beginners
Without validation, empty forms, typos in email addresses, or overly long text could slip straight into your database and cause confusing errors later. Laravel gives you a built-in helper called $request->validate() so you do not have to write dozens of manual if checks.
Writing Your First Validation Rules
Inside your Controller method, you can list simple rules for each form field using plain English words like required, email, or max:100:
<?php
public function store(Request $request)
{
// 1. Check the incoming form fields against simple rules
$cleanData = $request->validate([
'name' => 'required|min:2|max:80',
'email' => 'required|email',
'notes' => 'nullable|max:500',
]);
// 2. Only runs if every rule above passes!
Guest::create($cleanData);
return back()->with('success', 'Thank you! Your form was saved.');
}
If any rule fails, Laravel automatically sends the visitor back to the form with clear error messages—you do not even need to write redirect code.
Showing Polite Error Messages in Blade
To display the friendly warning right under an input box in your Blade template, use the @error directive and keep what the visitor already typed with old():
<label for="email">Email Address</label>
<input type="email" name="email" value="{{ old('email') }}">
@error('email')
<p class="error-text">{{ $message }}</p>
@enderror
3 Things to Remember
- Always validate before saving:
$request->validate()stops bad data at the door before it reaches your database. - Combine simple rules with a pipe (
|): Rules likerequired|emailare easy to read and stack together. - Use
old()and@errorin Blade: Visitors never lose what they typed when they fix a small mistake.