Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Verbose Validator Laravel Package

alikhosravidev/laravel-verbose-validator

Adds a verbose/trace mode to Laravel’s Validator to debug complex rules. Get step-by-step reports of each executed rule and its pass/fail outcome, with optional auto-enable via APP_DEBUG and configurable failure report types (failed/passed/all).

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require alikhosravidev/laravel-verbose-validator
    

    Publish the config (optional):

    php artisan vendor:publish --provider="AliKhosravi\VerboseValidator\VerboseValidatorServiceProvider"
    
  2. Basic Usage: Replace default Laravel validation with verbose output:

    use AliKhosravi\VerboseValidator\Facades\VerboseValidator;
    
    $validator = VerboseValidator::make($data, [
        'email' => 'required|email',
        'password' => 'required|min:8'
    ]);
    
    if ($validator->fails()) {
        // Returns detailed error messages with field paths
        dd($validator->errors()->all());
    }
    
  3. First Use Case: Replace a standard Validator::make() call in a form request or controller:

    // Before:
    $validator = Validator::make($request->all(), $rules);
    
    // After:
    $validator = VerboseValidator::make($request->all(), $rules);
    

Implementation Patterns

Common Workflows

  1. Form Request Integration: Extend Illuminate\Foundation\Http\FormRequest and override failedValidation():

    public function failedValidation(Validator $validator)
    {
        return response()->json([
            'success' => false,
            'errors' => VerboseValidator::make($this->all(), $this->rules())->errors()
        ], 422);
    }
    
  2. API Response Standardization: Create a base controller to wrap responses:

    public function respondWithValidation($validator)
    {
        return response()->json([
            'data' => null,
            'errors' => $validator->errors(),
            'status' => 'error'
        ], 422);
    }
    
  3. Dynamic Rule Generation: Combine with Laravel's Validator::extend() for custom rules while maintaining verbose output:

    Validator::extend('custom_rule', function ($attribute, $value, $parameters, $validator) {
        // Custom logic
        return true;
    });
    
    $validator = VerboseValidator::make($data, ['field' => 'custom_rule']);
    
  4. Nested Resource Validation: Leverage nested arrays with dot notation:

    $rules = [
        'user.address.city' => 'required|string',
        'user.address.postal_code' => 'required|digits:5'
    ];
    

Integration Tips

  • Middleware: Use App\Http\Middleware\ValidateVerbose to enforce verbose validation globally.
  • Testing: Mock VerboseValidator in unit tests:
    $validator = $this->partialMock(VerboseValidator::class, ['make']);
    $validator->shouldReceive('make')->andReturn($mockValidator);
    
  • Localization: Override error messages in resources/lang/en/validation.php while keeping verbose paths intact.

Gotchas and Tips

Pitfalls

  1. Rule Order Sensitivity:

    • Verbose paths are generated based on rule evaluation order. Reorder rules to match expected field paths:
      // May output: "user.address" instead of "user.address.city"
      $rules = ['user' => 'required', 'user.address.city' => 'required'];
      
  2. Custom Rule Paths:

    • Custom rules (Validator::extend()) may not inherit path context. Use Validator::replacer() to adjust:
      Validator::replacer('custom_rule', function ($message, $attribute, $rule, $parameters) {
          return str_replace(':attribute', 'custom_field', $message);
      });
      
  3. Nested Array Quirks:

    • Empty arrays in nested structures may break path generation. Use present rules to filter:
      $rules = ['user.*' => 'present|array', 'user.*.name' => 'required'];
      
  4. Performance Overhead:

    • Verbose validation adds minimal overhead (~5-10% in benchmarks), but avoid in bulk operations (e.g., CSV imports).

Debugging

  • Enable Debug Mode: Set VERBOSE_VALIDATOR_DEBUG=true in .env to log raw validation data to storage/logs/verbose-validator.log.

  • Path Resolution Issues: Check config/verbose-validator.php for path_resolver options. Customize with:

    'path_resolver' => \AliKhosravi\VerboseValidator\Resolvers\CustomPathResolver::class,
    

Extension Points

  1. Custom Path Resolvers: Implement AliKhosravi\VerboseValidator\Contracts\PathResolver to modify path generation:

    class CustomPathResolver implements PathResolver {
        public function resolve($attribute, $rules) {
            return str_replace('.', '_', $attribute);
        }
    }
    
  2. Error Formatter: Override the default formatter:

    'error_formatter' => \App\Services\CustomVerboseFormatter::class,
    
  3. Rule Presets: Create reusable rule sets with VerboseValidator::presets():

    VerboseValidator::preset('user', [
        'name' => 'required|string|max:255',
        'email' => 'required|email'
    ]);
    
    $validator = VerboseValidator::make($data, ['user' => VerboseValidator::preset('user')]);
    
  4. Conditional Rules: Use when/unless with verbose paths:

    $validator = VerboseValidator::make($data, [
        'password' => ['required', 'min:8', 'confirmed'],
        'password_confirmation' => ['required_with:password', 'same:password']
    ]);
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium