Laravel How to Make Custom Validator?

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company
# Laravel How to Make Custom Validators: Extending the Core Logic As developers working with Laravel, we often find ourselves in situations where the built-in validation rules aren't sufficient. We need to enforce complex business logic that requires custom checks beyond simple `required` or `string` validations. This is where extending core components, like `Illuminate\Validation\Validator`, becomes incredibly powerful. The confusion many developers face, as highlighted by the examples you referenced, is understanding how to define this custom logic and, more importantly, how to *invoke* it correctly within a standard request cycle. This guide will walk you through creating a custom validator and demonstrate the proper way to utilize it in your Laravel applications. ## Why Extend the Validator? The `Illuminate\Validation\Validator` class is the engine that processes all incoming data against defined rules. By extending it, we gain the ability to hook into this processing pipeline, allowing us to inject our own custom validation checks directly into the framework's flow. This approach promotes code reuse and keeps complex business rules encapsulated within dedicated classes, which aligns perfectly with the modular design principles championed by Laravel. ## Step 1: Creating the Custom Validator Class To start, we define our custom validator. For this example, let's create a validator that ensures a submitted product price is not negative and adheres to specific currency formatting rules. We will extend the base `Validator` class and override or add methods where our custom logic resides. ```php errors()->add('price', 'The price cannot be negative.'); } // Example: Custom currency formatting check if (isset($data['currency']) && !in_array($data['currency'], ['USD', 'EUR'])) { $this->errors()->add('currency', 'Unsupported currency selected.'); } } } ``` ## Step 2: Calling the Custom Validator Explicitly The key to solving your problem—how to call it—is understanding that you don't typically instantiate and run a custom validator directly in a controller. Instead, you integrate this logic into the main validation process, often by leveraging the `Validator` object itself or by defining rules that internally reference your custom logic. If you want to use this class as a standalone entity for specific data sets, you can instantiate it like any other class: ```php use App\Validation\CustomProductValidator; $data = [ 'price' => -10.00, 'currency' => 'GBP' ]; $customValidator = new CustomProductValidator(); $customValidator->validate($data); // Check for errors if ($customValidator->fails()) { // Errors will be available via $customValidator->errors() dd($customValidator->errors()); } else { echo "Validation successful!"; } ``` ### The Laravel Way: Integrating into Requests While the above demonstrates instantiating the class, in a typical Laravel application structure, you integrate this logic directly into your Request or Controller flow. You define your custom rules within the `rules()` method of your Form Request, and if needed, use a custom rule class (which is often preferred over extending the Validator directly for simple checks) to handle the actual execution. For more complex scenarios involving service layers or repository interactions—a common pattern when dealing with database integrity—you should ensure that your validation logic remains decoupled from the HTTP layer. This separation of concerns is vital, especially when scaling applications built on Laravel principles found at [laravelcompany.com](https://laravelcompany.com). ## Conclusion Creating custom validators by extending `Illuminate\Validation\Validator` gives you deep control over Laravel's core validation engine. While instantiating the class directly works for isolated testing, the most practical application in a full-stack environment is defining your custom logic within dedicated **Rule Objects** that