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.
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.
ValidatorChain, e.g., for a username field requiring alphanumeric characters and length constraints: $chain->addValidator(new Alpha())->addValidator(new StringLength(['min' => 3, 'max' => 20])).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.Validate\Each to validate all elements in an array (e.g., validating a list of email addresses in $_POST['emails']).AbstractValidator to implement domain-specific rules (e.g., UniqueUsername), and inject dependencies like repositories via constructor injection.getMessages() and getErrors() methods to collect and display validation feedback—especially useful in RESTful APIs where structured error responses are needed.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.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.setTranslator()—if you see raw message IDs, ensure a translator is configured before calling validate() (or use setTranslatorEnabled(true) explicitly if needed).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.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.getMessages() to assert on expected error codes.How can I help you explore Laravel packages today?