<?php
use Symfony\Component\Validator\ValidatorBuilder;
use Symfony\Component\Validator\Validator\RecursiveValidator;
use Symfony\Component\Validator\Constraints as C;
use Symfony\Component\Validator\ConstraintViolationList;
/* @var $validator RecursiveValidator */
$validator = (new ValidatorBuilder())
->getValidator();
$constraint1 = new C\Range(['min' => 3, 'max' => 9]);
$constraint2 = new C\Type(['type' => 'integer']);
$constraints = [$constraint1, $constraint2];
/* @var $valid ConstraintViolationList */
/* @var $invalid ConstraintViolationList */
// RecursiveValidator::validateValue is deprecated since 2.5, use validate
$valid = $validator->validate(4, $constraints);
echo get_violations($valid);
$invalid = $validator->validate(1, $constraints);
echo get_violations($invalid);
$invalid = $validator->validate('budgie', $constraints);
echo get_violations($invalid);
function get_violations(ConstraintViolationList $violations) {
$out = '<pre>violation count: ' . count($violations) . PHP_EOL;
foreach($violations as $v) {
$out .= print_r($v->getMessage(), true) . PHP_EOL;
}
$out .= '</pre>';
return $out;
}
violation count: 0
violation count: 1 This value should be between 3 and 9.
violation count: 2 This value should be a valid number. This value should be of type integer.