intermediateLaravel
How do you create a custom validation rule?
Answer
Clear, interview-ready explanation
Generate a rule object with php artisan make:rule followed by a descriptive name. Laravel places it in app/Rules, and the rule’s validate method receives the attribute name, value, and a failure callback that it calls when the value is invalid.
Add the rule object to a field’s validation rules alongside built-in rules. A closure rule is useful for a small one-off check, while a rule object is better when the logic needs reuse, tests, dependencies, or a clear domain name. If a rule must run for missing or empty input, create it as an implicit rule and define that behavior deliberately.