Validator
Symfony's constraint-based validation component for checking PHP values, objects, and forms
Repository Health
Technical Analysis
The Symfony Validator component implements value validation modeled on the JSR-303 Bean Validation specification: instead of writing imperative if checks, you declare constraints (NotBlank, Email, Length, Choice, and 150+ others) on class properties or standalone values using PHP attributes, YAML, XML, or annotations, then run a ValidatorInterface::validate() call that returns a ConstraintViolationList describing every failure.
Because it’s a standalone Symfony component with no hard framework dependency, it’s used both inside full Symfony applications (wired automatically through symfony/validator + the Form and HttpKernel integrations) and independently in any PHP project that needs structured, reusable validation rules — including popular API platforms and standalone DTOs. Custom constraints are added by pairing a Constraint class with a ConstraintValidator, and group sequences let the same object be validated differently depending on context (e.g., a “registration” vs. “profile update” group).
What You Get
- 150+ built-in constraints covering common cases (
NotBlank,Email,Length,Regex,Choice,Uuid,Json,Isbn, etc.) - PHP-attribute, YAML, XML, and static
loadValidatorMetadata()mapping for declaring constraints on classes - Validation groups and group sequences for context-dependent validation of the same object
- A
ConstraintViolationListresult type with property paths, messages, and codes suited for API error responses - Extension points (
ConstraintValidatorFactory, customConstraint/ConstraintValidatorpairs) for domain-specific rules
Common Use Cases
- Validating form submissions in Symfony applications before persisting entities
- Validating incoming API request payloads and returning structured field-level error responses
- Enforcing business rules on domain objects independent of any specific framework or ORM
- Building custom reusable constraints (e.g., a
UniqueEntityor business-specific rule) shared across a codebase
Under The Hood
Architecture - The component centers on a ValidatorBuilder-constructed Validator that walks a ClassMetadataFactory describing which constraints apply to which properties/methods, resolving each constraint to a ConstraintValidator via ConstraintValidatorFactory, and accumulating results into a ConstraintViolationList; metadata loaders (Mapping/Loader) support attributes, YAML, XML, and static-method declaration, all normalized into the same internal ClassMetadata representation. Tech Stack - Pure PHP 8.4+ with minimal hard dependencies (polyfill-ctype, polyfill-mbstring, translation-contracts), and optional dev-only integration with symfony/cache, symfony/property-access, symfony/expression-language, and egulias/email-validator for advanced constraints like Expression and RFC-compliant email checks. Code Quality - 256 test files exercise all 156 constraint classes plus the metadata loaders and validator core, following Symfony’s shared coding standards with typed properties and full PHPDoc author attribution; the sheer constraint count is kept manageable through a consistent Constraint/ConstraintValidator pairing convention. API Design - The public surface is small and consistent: instantiate constraints, call validate(), and inspect a violation list — the same shape whether validating a single scalar or a graph of nested objects, which keeps the learning curve low despite the breadth of built-in constraints.
Used by 3 apps in this directory
Kimai
Invoicing Finance · Project Management
Professional open-source time tracking with invoicing, multi-user support, SAML/LDAP auth, and a full REST API—self-host it or use the cloud.
Mautic
Automation · Marketing · Ecommerce
The world's largest open source marketing automation platform — own your data, run multi-channel campaigns, and escape vendor lock-in forever.
wallabag
Bookmarks Archiving
Self-hosted read-it-later app that saves clean, ad-free articles from any webpage for distraction-free reading across all your devices.