Validator

Symfony's constraint-based validation component for checking PHP values, objects, and forms

Library
Composer
vv8.1.4
2,680stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
87/100Excellent
Development Activity96
Maintenance96
Community56
Maturity60
Momentum40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
81/100Excellent
Architecture85
Code Quality88
Innovation72
Learning Curve78

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 ConstraintViolationList result type with property paths, messages, and codes suited for API error responses
  • Extension points (ConstraintValidatorFactory, custom Constraint/ConstraintValidator pairs) 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 UniqueEntity or 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.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search