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.
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.
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.Illuminate\Support\Facades\Validator and call validate() on a Request instance (e.g., $request->validate([...])) or Validator::make(...)->validate().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 formatting via Validator::extendImplicit() or replacer callbacks, and support multi-language messages via translation files (resources/lang/{locale}/validation.php).Validator::make(..., [], $customAttributes) to override human-readable field names for messages and avoid cryptic keys in errors.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.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.required_if, not required if), and field names use snake_case (e.g., first_name not firstName) unless aliased.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.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()).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.dd($validator->errors()) or $validator->failed() to inspect rule failures programmatically — especially helpful when building multi-step or conditional validation flows.How can I help you explore Laravel packages today?