{
  "id": 19776867,
  "title": "How to Create Custom Validation Rules in Laravel 13",
  "url": "https://urgent.news/2026/11/13/how-to-create-custom-validation-rules-in-laravel-13",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-11-13T08:09:25.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/muhammadyasiramin/how-to-create-custom-validation-rules-in-laravel-13-3bd2"
  },
  "original_language": "en",
  "account": "Creating custom validation rules in Laravel 13 involves running php artisan make:rule to generate a new rule class. This class must implement the ValidationRule interface and include a validate() method. The validate() method takes three parameters: $attribute (attribute name), $value (input value), and $fail (closure to store error messages).\n\nTo write the rule logic, first check if the input value is a string and meets the minimum length requirement. Then verify the value does not start with a digit and contains only letters, numbers, and underscores. If any condition fails, call $fail() to add an error message to the validation errors.\n\nIn a form request, use the custom validation rule by creating an array with the attribute name and the rule instance. For a closure rule, it can be used in place of a class and has the same signature. Place built-in validation rules like required before the custom rule to ensure the input meets basic criteria before running the custom logic.\n\nTo access other fields during validation, implement either DataAwareRule or ValidatorAwareRule interfaces and define the corresponding methods (setData() or setValidator()). These methods receive the validated data or validator instance, respectively, allowing the rule to access additional field values.\n\nTo handle translations, use the translate() method on the object returned by $fail() when creating an error message. This allows the error message to be translated using a language file key. Finally, to verify the custom rule works as expected, write a feature test that posts both valid and invalid input and checks for the expected error messages in the session.",
  "summary": "To create a custom validation rule in Laravel 13, run php artisan make:rule , implement the validate() method of the ValidationRule interface, call $fail() when the value is invalid, and add the rule object to your rules array. For one-off checks, a closure with the same signature works without a new class. What do you need before you start? Laravel 13 on PHP 8.3 or newer A form request or…",
  "key_points": [
    "Run php artisan make:rule to generate custom validation class",
    "Implement validate() method with $attribute, $value, $fail parameters",
    "Call $fail() to add error messages when validation fails"
  ],
  "editors_take": "Implementing specialized rule classes and interfaces gives developers the granular control necessary to enforce complex business logic and maintain data integrity within the Laravel framework.",
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "ai_generated": true,
  "disclaimer": "Summaries, key points and the editor’s take are written by software from other outlets’ reporting and may contain errors — always check the linked original."
}