How to Install the Laravel‑Validate Package: Step‑by‑Step Setup Guide
Run composer require milwad/laravel-validate to install the package, then publish the configuration with php artisan vendor:publish --tag="laravel-validate-config" to customize validation behavior.
The laravel‑validate package by milwad-dev provides a robust collection of custom validation rules for Laravel applications requiring PHP 8.0 or higher. Learning how to install the laravel‑validate package correctly ensures immediate access to utilities like phone number and URL validators through Laravel’s built‑in validation pipeline.
Prerequisites
Before you install the package, ensure your environment meets the requirements specified in composer.json. The package requires PHP ≥ 8.0 and Laravel ≥ 9.0 (or Laravel 5.5+ for older versions with manual service provider registration).
Step‑by‑Step Installation Guide
Install via Composer
Execute the installation command to add the package to your project dependencies:
composer require milwad/laravel-validate
This command updates your composer.json and downloads the source files, mapping the Milwad\LaravelValidate\ namespace to the src/ directory via PSR‑4 autoloading.
Publish the Configuration File
To customize validation options—such as enabling container‑based rule resolution or configuring custom phone validators—publish the default configuration:
php artisan vendor:publish --tag="laravel-validate-config"
This creates config/laravel-validate.php, where you can set flags like 'using_container' => true to use rule names directly in validation strings (e.g., 'required|ValidPhoneNumber').
Register the Service Provider (Laravel < 5.5)
Laravel 5.5 and newer versions auto‑discover the service provider automatically. For older versions, manually register Milwad\LaravelValidate\LaravelValidateServiceProvider::class in your config/app.php providers array. This registration is handled automatically in src/LaravelValidateServiceProvider.php, which binds all rule classes to the IoC container.
Publish Language Files (Optional)
For localized error messages, publish the translation files for your desired locale:
php artisan vendor:publish --tag="validate-lang-en"
Replace en with fa or other supported language codes. These files land in your resources directory, allowing you to customize messages defined in the package’s lang/ folder.
Verifying the Installation
Test the installation by implementing a rule in a controller. The following example uses ValidPhoneNumber from src/Rules/ValidPhoneNumber.php:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Milwad\LaravelValidate\Rules\ValidPhoneNumber;
class ContactController extends Controller
{
public function store(Request $request)
{
$request->validate([
'phone' => ['required', new ValidPhoneNumber()],
]);
return response()->json(['message' => 'Valid phone number']);
}
}
If you enabled 'using_container' => true in config/laravel-validate.php, you can alternatively use string‑based rules:
$request->validate([
'phone' => 'required|ValidPhoneNumber',
]);
Summary
- Install the package via
composer require milwad/laravel-validateto add it to your Laravel project. - Publish the configuration using
php artisan vendor:publish --tag="laravel-validate-config"to accessconfig/laravel-validate.php. - Enable
'using_container' => truein the config file to use rule names as strings instead of instantiating objects. - Optionally publish language files with
--tag="validate-lang-{locale}"for custom error messages. - Laravel 5.5+ auto‑discovers the service provider; older versions require manual registration in
config/app.php.
Frequently Asked Questions
What versions of PHP and Laravel does laravel‑validate support?
The package requires PHP 8.0 or higher and Laravel 9.0 or higher according to the composer.json constraints. However, Laravel 5.5 through 8.x are supported if you manually register Milwad\LaravelValidate\LaravelValidateServiceProvider::class in config/app.php.
Do I need to publish the configuration file to use the package?
No, publishing is optional. The package works immediately after Composer installation. However, publishing config/laravel-validate.php via vendor:publish is necessary if you want to customize the using_container setting or configure custom phone country validators.
How do I use validation rules without importing class names?
Set 'using_container' => true in your published configuration file. This allows you to write validation rules as strings like 'required|ValidPhoneNumber' instead of using new ValidPhoneNumber(), leveraging the container resolution implemented in src/LaravelValidateServiceProvider.php.
Can I customize the error messages for these validation rules?
Yes. Run php artisan vendor:publish --tag="validate-lang-en" (replacing en with your locale) to publish translation files. These files contain the default error messages used by rules in src/Rules/, which you can edit to match your application’s localization needs.
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 →