- How does this package improve Laravel’s native validation rules?
- It replaces string-based rules (e.g., `'required|string|min:2'`) with a fluent, type-safe API (e.g., `FluentRule::string()->required()->min(2)`). Each rule type only exposes relevant methods, reducing errors and enabling IDE autocompletion. Nested arrays are handled with `each()`/`children()`, keeping rules co-located instead of scattered across dot notation.
- Will this break existing Laravel validation logic?
- No—it’s designed as a drop-in replacement. Use the companion Rector package to automate migrations from string rules to fluent syntax. Start with non-critical validation (e.g., form requests) to test compatibility before full adoption.
- Does it support Laravel’s `Rule::when()` or `Rule::unless()`?
- Yes, but with a fluent twist. Use `when($condition, fn ($r) => $r->required())` or `unless($condition, fn ($r) => $r->string())` for conditional rules. The closure syntax ensures type safety and IDE hints for nested methods.
- How does the `HasFluentRules` trait improve performance?
- It optimizes wildcard validation (e.g., `items.*.id`) by reducing Laravel’s default O(n²) expansion to O(n) with batched processing. Benchmarks show up to 160x speedups for large arrays, ideal for bulk imports or API endpoints with deep nested validation.
- Can I use custom validation logic with this package?
- Yes, extend `FluentRule` or create custom rule builders. For dynamic rules, use closures: `FluentRule::custom(fn ($attr, $value) => YourCustomRule::validate($value))`. The package maintains compatibility with Laravel’s `Validator` extension methods.
- What Laravel versions and PHP requirements does it support?
- It requires **PHP 8.2+** and **Laravel 11+**. The modern PHP features (e.g., named arguments) enhance IDE support but aren’t mandatory for core functionality. Legacy projects may need a partial migration path.
- How does testing work with fluent validation?
- Test fluent rules like native Laravel validation. Use Pest or PHPUnit to assert rule chains: `FluentRule::string()->required()->min(2)->validate('test')` should throw an error. The package includes a `FluentRuleTestCase` helper for common assertions.
- Are there alternatives to this package for fluent validation?
- Laravel’s native validation is the baseline, but packages like `spatie/laravel-validation-rules` offer extensions. This package uniquely combines **type safety**, **IDE autocompletion**, and **performance optimizations** (e.g., `HasFluentRules`) not found in alternatives.
- How do I handle nested arrays or multi-dimensional validation?
- Use `each()` for flat arrays or `children()` for hierarchical data. Example: `FluentRule::array()->each(['id' => FluentRule::integer()->required()])`. For dynamic keys, combine with `when()` or `unless()` for conditional nesting.
- What’s the learning curve for teams new to fluent APIs?
- Minimal for Laravel devs—it mirrors Laravel’s existing validation patterns but removes string ambiguity. Start with simple rules (e.g., `FluentRule::string()->required()`) and gradually adopt `each()`/`children()` for complex cases. The Rector package automates bulk migrations.