- How do I install and set up milwad/laravel-validate in a Laravel 11 project?
- Run `composer require milwad/laravel-validate` to install. No additional setup is required for basic usage—just use rules like `ValidPhoneNumber` directly in your Form Requests or validation arrays. For container integration, add `'using_container' => true` to the config and resolve rules via the service container.
- Does this package work with Laravel’s built-in Form Request validation?
- Yes, it integrates seamlessly. Replace or extend your `rules()` method in Form Requests with package rules (e.g., `'phone' => ['required', 'ValidPhoneNumber']`). The package leverages Laravel’s native `Validator` facade, so no changes to your existing validation logic are needed.
- Can I use these rules in API validation (e.g., Laravel Sanctum or Passport)?
- Absolutely. Use the rules in your API resource validation or directly in controller methods with `Validator::make()`. For example, `Validator::make($data, ['credit_card' => ['required', 'ValidCreditCard']])` works identically to Form Requests.
- What Laravel and PHP versions are supported?
- The package supports Laravel 9 through 13 and PHP 8.0+. Check the [compatibility table](https://github.com/milwad-dev/laravel-validate#requirements) for version-specific notes. Upgrades between Laravel 9–13 are backward-compatible with no breaking changes.
- How do I add custom validation rules or extend existing ones?
- Extend the package’s `Rule` classes (e.g., `ValidPhoneNumber`) by creating your own class and overriding methods like `passes()`. Place it in `app/Rules` and reference it in your validation logic. The package’s OOP design makes this straightforward.
- Are there any performance concerns with using this package?
- Minimal overhead exists if you enable `using_container: true`, as it resolves rules dynamically. For high-throughput APIs, benchmark with `using_container: false` (static rules) to compare speeds. The package avoids heavy dependencies, so impact is typically negligible.
- Does this package support localization for validation messages?
- Yes, it includes built-in localization for multiple languages (e.g., English, Persian). Unsupported languages can be added via [GitHub PRs](https://github.com/milwad-dev/laravel-validate/pulls). Messages follow Laravel’s localization conventions, so they integrate with your existing `lang/validation.php` files.
- What happens if a rule like ValidJwt or ValidIban requires external libraries (e.g., firebase/php-jwt)?
- Some rules depend on third-party libraries (e.g., `ValidJwt` needs `firebase/php-jwt`). These must be installed separately via Composer. The package documents dependencies in its [README](https://github.com/milwad-dev/laravel-validate#requirements), so check before use.
- How do I test validation rules in my Laravel application?
- Test rules like any Laravel validation logic. Use PHPUnit to assert `Validator::make($data, ['field' => 'ValidRule'])->fails()` or `passes()`. For edge cases (e.g., false positives in `ValidCreditCard`), write custom tests or extend the rule’s logic to handle exceptions.
- Are there alternatives to this package for Laravel validation?
- Yes, consider Laravel’s native `Rule` objects (e.g., `Rule::required()->ValidPhoneNumber()`) for fluent chaining, or packages like `spatie/laravel-validation-rules` for additional rules. This package distinguishes itself with localization support, region-specific rules (e.g., `ValidIranPostalCode`), and seamless Laravel integration.