Skip to main content

Validation

The Validator class provides a simple, extensible way to validate input data.

Basic Usage

$validator = Validator::make($request->all(), [
'name' => 'required|string|max:255',
'email' => 'required|email|max:255',
'password' => 'required|min:8|confirmed',
]);

if ($validator->fails()) {
return redirect('/register')
->withErrors($validator->errors())
->withInput();
}

// Validation passed

Available Rules

RuleDescription
requiredField must be present and non-empty
emailMust be a valid email address
min:NMinimum length (string) or value (numeric) or count (array)
max:NMaximum length, value, or count
numericMust be a number
integerMust be an integer
stringMust be a string
booleanMust be true/false/0/1
alphaMay only contain letters
alpha_numMay only contain letters and numbers
urlMust be a valid URL
ipMust be a valid IP address
dateMust be a valid date (parsable by strtotime)
confirmedMust match {field}_confirmation
same:fieldMust match another field
different:fieldMust differ from another field
in:a,b,cMust be one of the given values
not_in:a,b,cMust not be one of the given values
arrayMust be an array
nullableField is optional (skips other rules when null)
regex:/pattern/Must match the regex pattern

Rule Arrays

Rules can also be specified as arrays:

$validator = Validator::make($request->all(), [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'email'],
'password' => ['required', 'min:8'],
]);

Custom Error Messages

$validator = Validator::make($request->all(), [
'email' => 'required|email',
], [
'email.required' => 'We need your email address.',
'email.email' => 'That does not look like a valid email.',
]);

Custom Rules

Validator::extend('strong_password', function ($field, $value, $params, $data) {
return (bool) preg_match('/[A-Z]/', $value) && strlen($value) >= 8;
}, 'The :attribute is not strong enough.');

// Usage
$validator = Validator::make($request->all(), [
'password' => 'required|strong_password',
]);

Validation Errors

$errors = $validator->errors();
// ['email' => ['The email field is required.'], 'name' => ['...']]

$first = $validator->firstError();
$first = $validator->firstError('email');

$all = $validator->all();
// ['The email field is required.', 'The name field is required.']

In Practice

A common pattern with redirects:

Route::post('/register', function ($request) {
$validator = Validator::make($request->all(), [
'name' => 'required|string|max:255',
'email' => 'required|email|max:255',
'password' => 'required|min:8|confirmed',
]);

if ($validator->fails()) {
return redirect('/register')
->withErrors($validator->errors())
->withInput();
}

$user = Auth::register($request->only(['name', 'email', 'password']));
Auth::login($user);
return redirect('/dashboard')->with('success', 'Account created!');
});

In views, errors are automatically displayed when flashed:

@if (session('_errors'))
<ul>
@foreach (session('_errors') as $field => $errors)
@foreach ($errors as $error)
<li>{{ $error }}</li>
@endforeach
@endforeach
</ul>
@endif