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

Validation Laravel Package

illuminate/validation

Core Laravel validation component providing a fluent, rule-based validator for arrays and request input. Supports built-in and custom rules, conditional validation, messages and attributes, error bags, and translation-ready output for consistent data validation across apps.

View on GitHub
Deep Wiki
Context7

Getting Started

Begin by installing via Composer if using Laravel (it’s already included by default in new Laravel apps). The core entry point is Validator::make($data, $rules, $messages), typically invoked via form requests or directly in controllers. Start with defining simple rules using string-based syntax (e.g., 'email' => 'required|email') or via the fluent Rule builder for complex cases. The Laravel validation documentation is the primary resource — don’t skip the sections on error messaging and the ValidationException behavior. First real-world use case: validating an incoming web request in a controller method before persisting data.

Implementation Patterns

  • Form Requests: Use php artisan make:request StoreUserRequest to generate dedicated validation classes — this promotes reusability and keeps controllers thin. Override authorize() and rules()/messages() for context-aware logic.
  • Inline Validator: For simple or dynamic rules, inject Illuminate\Support\Facades\Validator and call validate() on a Request instance (e.g., $request->validate([...])) or Validator::make(...)->validate().
  • Custom Rules: Implement Rule objects (e.g., Rule::requiredIf(...)) or rule classes (php artisan make:rule Uppercase) for domain-specific constraints. These compose cleanly with Laravel’s existing rules.
  • Attribute Replacer & Messages: Customize :attribute formatting via Validator::extendImplicit() or replacer callbacks, and support multi-language messages via translation files (resources/lang/{locale}/validation.php).
  • Batch Validation: Use Validator::make(..., [], $customAttributes) to override human-readable field names for messages and avoid cryptic keys in errors.

Gotchas and Tips

  • String vs Array Rules: Avoid mixing string-based rules with arrays containing Rule objects — the former is evaluated as a single pipe-delimited string, which can mask syntax errors or unexpected behavior (e.g., nested pipes break parsing). Prefer array syntax for complex rules.
  • Hidden Fields & sometimes(): Rules applied to empty or missing fields won’t run unless marked required or wrapped in sometimes. Be explicit: sometimes|required|string vs sometimes|nullable|string.
  • Custom Messages in Array Format: Custom messages must match the exact rule name (required_if, not required if), and field names use snake_case (e.g., first_name not firstName) unless aliased.
  • Validation Timing & Early Return: Laravel throws ValidationException on failure — which triggers JSON responses in APIs or redirects in web routes — but not when using validateOrThrow() or validateWithBag() in newer versions. Prefer explicit validation to avoid silent fallbacks.
  • Extension Points: Extend the validator via Validator::extend() or custom rule classes — ideal for legacy regex patterns, third-party API checks, or reusable business constraints. Register extensions in a service provider (e.g., AppServiceProvider::boot()).
  • Numeric vs String Comparisons: Rules like gt:5 compare numerically; string + numeric rules are mutually exclusive. Use sometimes/nullable to avoid validation errors on optional numeric fields that may be missing or empty strings.
  • Debugging Tip: Use dd($validator->errors()) or $validator->failed() to inspect rule failures programmatically — especially helpful when building multi-step or conditional validation flows.
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.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport
twbs/bootstrap4