How to Enable Container-Based Registration for Rules in Laravel-Validate
Enable container-based registration by setting 'using_container' => true in the config/laravel-validate.php file to automatically register all validation rules from the src/Rules directory.
The milwad-dev/laravel-validate package ships with a comprehensive collection of custom validation rules located in src/Rules. By default, these rules are not automatically available to Laravel's validator; you must enable the container-based registration feature to auto-wire them without manual Validator::extend calls.
How Container-Based Registration Works
The registration mechanism is controlled by the LaravelValidateServiceProvider located at src/LaravelValidateServiceProvider.php. When enabled, the provider dynamically discovers and registers rule classes during the application boot cycle.
The Configuration Flag
The feature is governed by the using_container boolean flag in the package configuration. By default, this value is set to false at line 98 of config/laravel-validate.php:
// config/laravel-validate.php
'using_container' => false, // default value
When switched to true, the service provider automatically scans the rules directory and registers each class with Laravel's validator.
The Registration Process
The automatic registration occurs in the provider's boot() method (lines 75-88). The process follows this architectural flow:
-
Configuration Merge – During
register(), the provider merges the default configuration into the host application at line 25. -
Boot Phase Check – At line 75, the provider reads the
using_containervalue to determine if auto-registration should execute. -
Rule Discovery – If enabled, the provider gathers all rule files using
File::files(__DIR__.'/Rules')at line 77. -
Dynamic Registration – For each file discovered, the provider:
- Constructs the fully-qualified class name as
Milwad\LaravelValidate\Rules\{FileName}(line 80) - Calls
Validator::extendwith a closure that instantiates the rule and forwards the validation call (lines 82-86)
- Constructs the fully-qualified class name as
Once complete, all rules in the src/Rules directory are instantly available as validation strings (e.g., ValidPhone, ValidUuid).
Enabling Container-Based Registration
Follow these steps to activate automatic rule registration in your Laravel application.
Step 1: Publish the Configuration
If you have not already published the package configuration, run the following Artisan command:
php artisan vendor:publish --tag=laravel-validate-config
This copies the configuration file to config/laravel-validate.php.
Step 2: Update the Configuration
Open the published configuration file and set the using_container flag to true:
// config/laravel-validate.php
'using_container' => true, // ✅ enable automatic rule registration
This change instructs the LaravelValidateServiceProvider to scan and register all rule classes on the next application boot.
Step 3: Clear Configuration Cache
If your application uses configuration caching, clear the cache to ensure the new setting takes effect:
php artisan config:clear
Using Auto-Registered Rules
With container-based registration enabled, you can reference any rule from the src/Rules directory directly by its filename (without the .php extension). No additional service provider modifications or manual Validator::extend calls are required.
use Illuminate\Support\Facades\Validator;
$payload = [
'email' => 'example@domain.com',
'phone' => '+1 555 1234567',
'uuid' => '550e8400-e29b-41d4-a716-446655440000',
];
$validator = Validator::make($payload, [
'email' => 'required|email',
'phone' => 'required|ValidPhone', // resolves to Milwad\LaravelValidate\Rules\ValidPhone
'uuid' => 'required|ValidUuid', // resolves to Milwad\LaravelValidate\Rules\ValidUuid
]);
if ($validator->fails()) {
// handle validation errors
}
The rules are resolved at runtime from the container, allowing you to focus on validation logic rather than registration boilerplate.
Summary
- Container-based registration is controlled by the
using_containerflag inconfig/laravel-validate.php. - When enabled, the
LaravelValidateServiceProviderautomatically scanssrc/Rulesand registers each class viaValidator::extendduring the boot phase. - The default value is
false, requiring manual registration or enabling the feature for auto-discovery. - Rules are referenced by their filename (e.g.,
ValidPhone) and are resolved using theMilwad\LaravelValidate\Rulesnamespace.
Frequently Asked Questions
What is the default value of using_container?
The default value is false, as defined at line 98 of config/laravel-validate.php. This means the package does not automatically register validation rules until you explicitly enable the feature.
Do I need to manually call Validator::extend when using container-based registration?
No. When using_container is set to true, the LaravelValidateServiceProvider handles all Validator::extend calls automatically during the boot process (lines 82-86 of the service provider). You can use the rules immediately without additional setup.
Where are the rule classes located?
All validation rule classes are stored in the src/Rules directory within the package source. The service provider scans this directory using File::files(__DIR__.'/Rules') to discover available rules for registration.
Can I use container-based registration alongside manually registered rules?
Yes. Enabling container-based registration does not prevent you from manually calling Validator::extend for custom rules or extending the package's rules with additional behavior. Both registration methods work concurrently without conflict.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →