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 Validation Laravel Package

prettus/laravel-validation

Laravel package providing a reusable validation service via LaravelValidator classes. Define rules for create/update, customize error messages and attribute names, and throw ValidatorException when validation fails—handy for keeping validation logic organized outside controllers.

View on GitHub
Deep Wiki
Context7

Getting Started

Install via Composer: composer require prettus/laravel-validation. Create a validator class extending Prettus\Validator\LaravelValidator, defining rules for RULE_CREATE and/or RULE_UPDATE. In controllers, inject the validator (and repository if using the Prettus Repository pattern), then call $validator->with($request->all())->passesOrFail(ValidatorInterface::RULE_CREATE) before persisting data. Wrap in a try/catch for ValidatorException to handle validation failures—typically returning JSON error responses in APIs.

Note for Laravel 13.x users: This package now supports Laravel 13.x (release 1.8.0). No additional configuration is required beyond standard installation.


Implementation Patterns

  • Contextual validation: Define separate rule sets per operation (e.g., title required on create, optional on update for non-required fields).
  • Reusability & inheritance: Create base validators (e.g., BaseApiValidator) with shared attributes/messages, and extend them for domain-specific validators.
  • API error handling: In controllers, catch ValidatorException and extract field-specific messages via $e->getValidator()->getMessages() to return structured error responses (e.g., ['email' => ['The email field is required.']]).
  • Dependency injection: Bind validators via Laravel’s container (auto-wiring or explicit binding) to support testing and decoupling—avoid resolving via Validator::make() directly.
  • Composing with Laravel rules: Use Laravel’s native rules (e.g., sometimes, exclude) and custom rule objects inside rule strings ('email' => 'required|email|unique:users'), or mix with ValidationRule objects where supported.

Gotchas and Tips

  • Input must be explicitly set: Always call with($input) before passesOrFail()—validation on unbound input silently succeeds with empty data.
  • Rules are not inherited across contexts: If RULE_UPDATE omits a field present in RULE_CREATE, that field is not validated in updates—explicitly declare all required rules per context.
  • Custom messages require dot notation for field-specific overrides: Use 'email.required' not 'email: required'; global messages (e.g., 'required') apply to all fields.
  • ValidatorException holds full validator state: Access the underlying Laravel validator via $e->getValidator() to inspect raw errors, rules, or attributes for advanced error reporting.
  • Laravel 13.x support: This package now officially supports Laravel 13.x (release 1.8.0). No breaking changes were introduced, and existing codebases should work seamlessly.
  • Override validator() for customization: Extend the validator factory if you need custom validation behavior (e.g., translatable attributes, custom rule sets), though this is rarely needed.
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.
emuniq/filament-browser-notifications
syriable/filament-translator
hungnm28/livewire-form
wenprise/eloquent
crudly/encrypted
fadion/bouncy
cuci/prototurk-sdk
gos/pubsub-router-bundle
cuci/prototurk-sdk-symfony
clementtalleu/easyadmin-markdown-bundle
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui