Configuration Options for Laravel-Validate: The Complete Guide
Laravel-Validate exposes four primary configuration options in config/laravel-validate.php—phone-country, landline-country, using_container, and countries—which control phone validator mappings, container-based rule registration, and supported country lists.
The milwad-dev/laravel-validate package centralizes its behavior through a single configuration file. Understanding these configuration options for Laravel-Validate allows you to customize phone validators, enable string-based rule syntax, and extend the package's country database without modifying core source code.
Core Configuration Options in config/laravel-validate.php
The package reads all settings from config/laravel-validate.php, which is loaded by LaravelValidateServiceProvider::boot() at lines 63-74. Four keys govern every customizable behavior.
Phone Country Mapping (phone-country)
The phone-country array (lines 47-66) maps ISO-2 country codes to validator classes. Each entry points to a concrete implementation of Milwad\LaravelValidate\Utils\CountryPhoneValidator that exposes a validate(string $phone): bool method.
When the ValidPhoneNumber rule executes, the package resolves the appropriate validator by reading config('laravel-validate.phone-country') and passing the mapping to CountryPhoneCallback::addValidator().
Landline Country Mapping (landline-country)
Operating identically to phone mapping, the landline-country array (lines 74-93) configures validators for landline numbers. This separation allows distinct validation logic for mobile versus fixed-line formats within the same country.
Container Registration (using_container)
The boolean using_container option (line 98) controls how rules are registered with Laravel's Validator.
When set to true, the service provider scans the src/Rules directory and registers every rule via Validator::extend(), enabling string-based validation syntax such as required|ValidPhone. When false (default), you must instantiate rules explicitly with new.
Country List (countries)
The countries array (lines 105-263) provides an ISO-2-to-name mapping used by the ValidCountry rule. You can append missing territories or override existing names without altering the package source.
How Configuration Options Are Consumed at Runtime
During the provider's boot() method, the package resolves configuration values through Laravel's config() helper and wires them into internal callbacks.
For phone and landline validation, the mappings are retrieved via config('laravel-validate.phone-country') and config('laravel-validate.landline-country'), then passed to CountryPhoneCallback::addValidator() and CountryLandlineCallback::addValidator() respectively. These callbacks resolve the appropriate validator class when a rule executes.
When using_container is enabled, the provider iterates through src/Rules, registers each rule with Validator::extend(), and allows Laravel to resolve rule instances from the container based on the configuration.
Extending and Customizing Configuration
To modify any configuration option, first publish the configuration file to your application.
Publishing the Configuration
Run the following Artisan command:
php artisan vendor:publish --tag="laravel-validate-config"
This copies config/laravel-validate.php into your project's config directory, where changes are automatically loaded on the next request.
Adding a Custom Phone Validator
Extend the phone-country array to support a custom territory:
// config/laravel-validate.php
'phone-country' => [
// existing entries...
'XY' => \App\Validators\XYPhoneValidator::class,
],
Your custom class must implement Milwad\LaravelValidate\Utils\CountryPhoneValidator and expose a validate(string $phone): bool method.
Enabling String-Based Rule Syntax
Toggle container registration to use validation strings:
// config/laravel-validate.php
'using_container' => true,
Now you can write validation rules as strings:
$request->validate([
'phone' => 'required|ValidPhone',
'vat' => 'required|ValidVatId',
]);
Extending the Country List
Append missing territories to the countries array:
// config/laravel-validate.php
'countries' => [
// existing entries...
'XY' => 'Xyland',
],
The ValidCountry rule will now recognize "Xyland" as a valid value.
Summary
- Configuration file: All options reside in
config/laravel-validate.php, loaded byLaravelValidateServiceProvider::boot(). - Phone and landline mapping: The
phone-countryandlandline-countryarrays map ISO-2 codes to validator classes implementingCountryPhoneValidator. - Container registration: Setting
using_containertotrueenables string-based validation syntax by registering rules withValidator::extend(). - Country list: The
countriesarray powers theValidCountryrule and can be extended with custom territories. - Customization: Publish the config using
php artisan vendor:publish --tag="laravel-validate-config"to override any option.
Frequently Asked Questions
How do I publish the Laravel-Validate configuration file?
Run the Artisan command php artisan vendor:publish --tag="laravel-validate-config". This copies the default configuration from the package into your application's config/laravel-validate.php, where you can safely modify settings without affecting the vendor directory.
What is the purpose of the using_container configuration option?
The using_container boolean determines how validation rules are registered with Laravel. When set to true, the LaravelValidateServiceProvider scans the src/Rules directory and registers each rule via Validator::extend(), allowing you to use string-based validation like required|ValidPhone. When false (default), you must instantiate rules directly with the new keyword.
Can I add custom country validators for phone numbers?
Yes. Add an entry to the phone-country or landline-country arrays in your published config, mapping an ISO-2 country code to a custom class. Your class must implement Milwad\LaravelValidate\Utils\CountryPhoneValidator and provide a validate(string $phone): bool method. The package automatically uses your validator when the corresponding country code is specified.
How does the countries configuration affect validation?
The countries array provides the data source for the ValidCountry rule, mapping ISO-2 codes to country names. When validating a field with the ValidCountry rule, the package checks the input against the values in this array. You can extend the list by adding new entries to the config, allowing validation of additional territories or custom country names without modifying the package source.
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 →