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

Laminas Validator Laravel Package

laminas/laminas-validator

Laminas Validator provides flexible, reusable validation rules for PHP applications. Includes built-in validators, input filtering/validation chains, and tools for validating common data types like emails, URLs, numbers, strings, and more.

View on GitHub
Deep Wiki
Context7

Getting Started

Install via Composer: composer require laminas/laminas-validator. Start by using built-in validators for common tasks—e.g., validating an email with new EmailAddress() or a string length with newStringLength(['min' => 3, 'max' => 50]). For quick validation of a single value, use validate($value) on any validator instance. Begin with simple use cases like form input sanitization or API request parameter validation before introducing validator chains. Check the docs/ directory in the repository for quickstart guides and a full list of built-in validators.

Implementation Patterns

  • Chaining Validators: Combine multiple validators with ValidatorChain, e.g., for a username field requiring alphanumeric characters and length constraints: $chain->addValidator(new Alpha())->addValidator(new StringLength(['min' => 3, 'max' => 20])).
  • Aggregate Validation: Use ValidatorPluginManager (especially in Laminas MVC) to manage and reuse validator instances across the app. In non-MVC contexts, manually instantiate and compose validators in service classes or DTOs.
  • Array Validation: Use Validate\Each to validate all elements in an array (e.g., validating a list of email addresses in $_POST['emails']).
  • Custom Validators: Extend AbstractValidator to implement domain-specific rules (e.g., UniqueUsername), and inject dependencies like repositories via constructor injection.
  • Error Aggregation: Leverage getMessages() and getErrors() methods to collect and display validation feedback—especially useful in RESTful APIs where structured error responses are needed.

Gotchas and Tips

  • Validator Chain Short-Circuiting: By default, ValidatorChain stops on first failure unless breakOnFailure(false) is set—use setBreakOnFailure() per validator to fine-tune. Misconfigured breaks can lead to misleading error counts.
  • Array Context Sensitivity: Some validators (e.g., Date, GreaterThan) expect scalar inputs; feeding arrays silently fails unless breakOnFailure is disabled and errors are carefully aggregated. Always validate key-by-key for associative data.
  • Translation Strings: Default error messages are translatable via setTranslator()—if you see raw message IDs, ensure a translator is configured before calling validate() (or use setTranslatorEnabled(true) explicitly if needed).
  • File Uploads: Use File\Size and File\Extension after File\IsUpload to validate real upload files—avoid validating raw $_FILES['field']['tmp_name'] directly without prior upload checks.
  • Extensibility Points: Override getMessages() to customize error formatting or add context-aware messages. When extending validators, use getValue() (not direct property access) to respect validation filters applied upstream.
  • Testing Tip: Validator classes are stateless by design—test them with data providers covering edge cases (e.g., null, empty string, UTF-8, boundary values). Use getMessages() to assert on expected error codes.
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