Laravel Validation Rules Reference Guide
A Laravel validation rules reference guide for validating request data with rules, custom messages, and Form Requests.
Laravel Reference
Fundamentals
Data
Validation & Security
Automation & Communication
Validating in a controller
public function store(Request $request)
{
$validated = $request->validate([
'title' => 'required|string|max:255',
'email' => 'required|email|unique:users,email',
'age' => 'nullable|integer|min:18',
]);
Post::create($validated);
}
If validation fails, Laravel automatically redirects back with the errors flashed to the session (for a web request) or returns a 422 JSON response with the error details (for an API request) — nothing else to write.
Common rules
| Rule | Checks |
|---|---|
required | Present and not empty. |
nullable | Allows the field to be missing or null — skips other rules when it is. |
sometimes | Only validates the field when it's actually present in the input. |
string / integer / numeric / boolean / array | Must be that PHP type. |
email | Must be a validly formatted email address. |
unique:users,email | No existing row in users.email matches this value. |
exists:users,id | A row with this value must already exist in users.id. |
min:8 / max:255 | Minimum/maximum length (strings), value (numbers), or item count (arrays). |
between:1,100 | Value or length falls within the given range. |
confirmed | A matching {field}_confirmation field must be present (e.g. password confirmation). |
date / date_format:Y-m-d | Must be a valid date, optionally in a specific format. |
in:draft,published,archived | Value must be one of the given options. |
regex:/^[A-Z]+$/ | Must match the given regular expression. |
file / image / mimes:jpg,png,pdf | Uploaded file constraints, including allowed MIME types. |
required_if:type,business | Required only when another field has a given value. |
same:password / different:old_password | Must (or must not) match another field's value. |
Custom error messages
$request->validate([
'title' => 'required|max:255',
], [
'title.required' => 'Please give your post a title.',
'title.max' => 'Titles cannot be longer than 255 characters.',
]);
Form Requests
For anything beyond a couple of rules, move validation into its own class so the controller stays focused on the actual logic.
php artisan make:request StorePostRequest
class StorePostRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}
public function rules(): array
{
return [
'title' => ['required', 'string', 'max:255'],
'body' => ['required', 'string'],
];
}
public function messages(): array
{
return [
'title.required' => 'Please give your post a title.',
];
}
}
public function store(StorePostRequest $request)
{
// Already validated by the time this method runs
Post::create($request->validated());
}
Manual validator instances
Useful outside of a controller/request, e.g. validating an array in a job or console command.
use Illuminate\Support\Facades\Validator;
$validator = Validator::make($data, [
'email' => 'required|email',
]);
if ($validator->fails()) {
return redirect()->back()->withErrors($validator)->withInput();
}