Validation Rules in Laravel define constraints for input data to ensure it meets specific requirements, such as being required, numeric, or properly formatted. These rules are applied during user input validation.
Validation Rules are a core feature of Laravel, designed to simplify input validation while maintaining clean and readable code.
To validate a request:
$request->validate([
'email' => 'required|email',
'password' => 'required|min:8',
]);
For more complex validation, use a Form Request:
php artisan make:request RegisterUserRequest
In the request class:
public function rules()
{
return [
'name' => 'required|string',
'email' => 'required|email|unique:users,email',
'password' => 'required|min:8',
];
}
This ensures consistent and reusable validation logic.