Symfony/Validator for PHP: Complete Guide with Examples
\n\n1. What It Is and Why You Need It
\n\nThe Symfony/Validator library is a powerful and flexible component of the Symfony ecosystem, designed for data validation. Unlike simple "on-the-fly" checks, this component offers a declarative approach: you describe validation rules (constraints) using annotations, PHP 8+ attributes, YAML, XML, or PHP arrays, and then pass the data to a special service — ValidatorInterface. This allows you to separate validation logic from the application's business logic, making the code cleaner, more testable, and reusable.
Why is this needed? In any web application (and beyond), data comes from untrusted sources: website forms, API requests, CSV file imports. Without validation, you risk SQL injections, XSS attacks, incorrect calculations, or simply "garbage" in the database. Symfony/Validator solves these problems comprehensively: it checks data types, string lengths, number ranges, email uniqueness, regular expression matching, and much more. Moreover, it can collect all errors into a single ConstraintViolationList object, which can be easily converted into a JSON response for an API or displayed as a list of errors on a form.
The component is completely independent of the Symfony framework. You can use it in any PHP project (Laravel, Yii, your own framework, or even "plain" PHP). It is actively maintained, has excellent documentation, and a huge community. It is the "gold standard" for validation in the PHP world.
\n\n2. Installation
\n\nInstallation is done via Composer. Run the following in your terminal at the root of your project:
\n\ncomposer require symfony/validator\n\nIt is also recommended to install the symfony/property-access component for proper work with nested objects and arrays:
composer require symfony/property-access\n\nIf you plan to use annotations/attributes, make sure you have doctrine/annotations installed (for PHP 7.x) or you are using PHP 8+ with native attributes.
3. Quick Start — Minimal Working Example
\n\nLet's create a simple User class with validation attributes and validate it.
<?php\n// 1. Include Composer autoloader\nrequire_once 'vendor/autoload.php';\n\n// 2. Import necessary classes\nuse Symfony\\Component\\Validator\\Validation;\nuse Symfony\\Component\\Validator\\Constraints as Assert;\n\n// 3. Create a class with validation attributes (PHP 8+)\nclass User {\n #[Assert\\NotBlank(message: "Name cannot be empty")]\n #[Assert\\Length(min: 2, max: 50, minMessage: "Name must be at least {{ limit }} characters")]\n public string $name;\n\n #[Assert\\NotBlank]\n #[Assert\\Email(message: "Email '{{ value }}' is not valid.")]\n public string $email;\n\n #[Assert\\PositiveOrZero]\n public int $age;\n}\n\n// 4. Create the validator\n$validator = Validation::createValidatorBuilder()\n ->enableAttributeMapping() // Enable attribute support\n ->getValidator();\n\n// 5. Create an object with invalid data\n$user = new User();\n$user->name = 'A'; // Name too short\n$user->email = 'not-an-email';\n$user->age = -5;\n\n// 6. Validate\n$violations = $validator->validate($user);\n\n// 7. Handle errors\nif (count($violations) > 0) {\n foreach ($violations as $violation) {\n echo $violation->getPropertyPath() . ': ' . $violation->getMessage() . "\\n";\n }\n} else {\n echo "Data is valid!";\n}\n\n\nThis example will output:
\nname: Name must be at least 2 characters\nemail: Email 'not-an-email' is not valid.\nage: This value should be either positive or zero.\n\n4. Main Methods/Functions/Classes
Also in library
Data Validation in C# (.NET): Libraries, Attributes, and FluentValidation
OAuth client for Kotlin: integration with providers via ktor-client and kotlinx-serialization
Rust Standard Library: Complete Overview
Lottie for Swift: Complete Guide with Examples
JSON Processing in Java: Jackson and Gson Libraries for Working with Data