Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
An example of standalone usage of the symfony validator component
# ignore certain files
# ignore compiled files
# system files
# ignore Sass cache
"name": "chrisguitarguy/symfony-validation-example",
"description": "Standalone validator usage example.",
"keywords": ["symfony", "validator"],
"require": {
"php": ">=5.5",
"symfony/validator": "~2.6",
"symfony/config": "~2.6"
"autoload": {
"psr-4": {
"Chrisguitarguy\\ValidatorExample\\": "."
require __DIR__.'/vendor/autoload.php';
use Symfony\Component\Validator\Validation;
use Chrisguitarguy\ValidatorExample\User;
$validator = Validation::createValidatorBuilder()
$user = new User('notAnEmail', null);
$errors = $validator->validate($user);
foreach ($errors as $error) {
echo $error->getPropertyPath(), ': ', $error->getMessage(), PHP_EOL;
// or for debugging:
echo $error, PHP_EOL;
$validUser = new User('', 'aPassword');
$errors = $validator->validate($validUser);
echo count($errors), PHP_EOL;
namespace Chrisguitarguy\ValidatorExample;
final class User
private $email;
private $password;
public function __construct($email, $password)
$this->email = $email;
$this->password = $password;
public function getEmail()
return $this->email;
public function getPassword()
return $this->password;
<?xml version="1.0" encoding="UTF-8" ?>
<constraint-mapping xmlns=""
<class name="Chrisguitarguy\ValidatorExample\User">
<property name="email">
<constraint name="NotBlank" />
<constraint name="Email" />
<property name="password">
<constraint name="NotBlank" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.