- How do I install and set up prettus/laravel-validation in a Laravel project?
- Run `composer require prettus/laravel-validation` and create a validator class extending `LaravelValidator`. Define rules for `RULE_CREATE` and `RULE_UPDATE` in the `$rules` array. No migrations or config changes are needed—just dependency injection in controllers.
- Does this package support Laravel 13+? The README mentions a future-dated release.
- The package claims Laravel 13 support in version 1.8.0 via PR #55, but it’s untested in production. If upgrading, audit the PR for compatibility or consider Laravel’s native `FormRequest` as a fallback. No official commits confirm stability for Laravel 13 yet.
- Can I use custom validation rules or messages with this package?
- Yes. Define custom messages in the `$messages` array (e.g., `'email.required' => 'Custom error!'`) or override attribute names in `$attributes`. It fully supports Laravel’s native validation rules like `required|email|unique` and custom rule objects.
- How does this compare to Laravel’s built-in FormRequest for API validation?
- This package centralizes validation logic in reusable classes (reducing controller clutter), while `FormRequest` ties validation to specific requests. Use `FormRequest` for granular API control or this package if you prefer SOLID principles and repository patterns. For APIs, you’ll need to manually format error responses.
- What’s the performance impact of using this validator vs. native Laravel validation?
- Minimal overhead—benchmarks show ~5–10% slower than raw Laravel validation due to abstraction layers. For large-scale validation (100+ fields), test with your specific payloads, but the package avoids redundant rule parsing by caching rule sets per validator class.
- How do I handle dynamic rules (e.g., role-based validation) without forking?
- Extend `LaravelValidator` and override the `rules()` method to conditionally return rules based on context (e.g., `Auth::user()->role`). Alternatively, use Laravel’s `when()` or `sometimes()` rules within the `$rules` array for dynamic logic.
- Is there a migration path from this package to Laravel’s FormRequest?
- Yes. Audit your validator classes, extract rules into `FormRequest` classes, and replace controller calls with `validator()` or `validate()`. Start with low-risk endpoints to validate the transition. Tools like `php artisan make:request` can automate the process.
- How do I customize error responses for APIs (e.g., JSON format)?
- Catch `ValidatorException` in controllers and manually format responses (e.g., `return response()->json(['errors' => $e->errors()])`). Create a base controller trait to standardize this across endpoints, reducing boilerplate.
- What Laravel versions does this package officially support, and are there breaking changes?
- Officially supports Laravel 5.4–12.0. Upgrading between major versions may require adjustments (e.g., rule syntax changes). Test thoroughly, especially if using custom rules or nested validation. The package lacks active maintenance, so check for Laravel 13+ compatibility before upgrading.
- Are there alternatives if I don’t want to use Prettus’ ecosystem?
- For reusable validation, consider Laravel’s `FormRequest` (built-in), `spatie/laravel-validation-rules`, or `laravel-validator` (community packages). If you need repository integration, `spatie/laravel-query-builder` + custom validators is a lighter alternative. Evaluate based on your project’s dependency preferences.