Validating Input Data
Accepting user input is only safe once it has been checked against clear rules. Laravel's validation system makes this straightforward, offering a large set of built-in rules along with automatic error handling for the browser-facing side of an application.
Validating Inside a Controller
The most common approach is calling the request's validate() method directly inside a controller, passing an array of field names mapped to validation rules. If validation fails, Laravel automatically redirects back to the previous page with the errors and old input flashed to the session.
Common Validation Rules
| Rule | What It Checks |
|---|---|
required | The field must be present and not empty |
email | The value must be a properly formatted email address |
min:8 / max:255 | Enforces a minimum or maximum length |
unique:users,email | The value must not already exist in a given table/column |
confirmed | Requires a matching *_confirmation field, common for passwords |
Displaying Errors in a View
- Laravel automatically shares an
$errorsvariable with every view - Blade's
@errordirective checks for a specific field's error message - Old input can be repopulated into form fields using the
old()helper
Built-in rules cover most situations, but applications often need checks that are specific to their own business logic — which is where custom validation rules and messages come in.
Ready to master Laravel Training Course?
Join Uncodemy's hands-on Laravel program and build real, production-ready applications.
