Custom validation rules:
Create Rule
php artisan make:rule UppercaseRule Class
class Uppercase implements ValidationRule
{
public function validate(string $attribute, mixed $value, Closure $fail): void
{
if (strtoupper($value) !== $value) {
$fail('The :attribute must be uppercase.');
}
}
}Use Rule
$request->validate([
'name' => ['required', new Uppercase],
]);Closure Rule
'email' => [
function ($attribute, $value, $fail) {
if (!str_ends_with($value, '@company.com')) {
$fail('Must be a company email.');
}
},
],