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.

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.
  • No native Laravel 13 support yet: Current release (v1.4.0) targets Laravel up to v12—verify compatibility or consider migrating to Laravel’s native FormRequest for future-proofing.
  • 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.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
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