<?php
use Symfony\Component\Validator\ValidatorBuilder;
use Symfony\Component\Validator\Constraints as C;
use Symfony\Component\Validator\ConstraintViolationList;
use Doctrine\Common\Annotations\AnnotationRegistry;
$projectRootPath = '/path/to/project';
/**
* @C\Expression(
* expression="this.start < this.end",
* message="Why is you start value bigger than the end value?"
* )
*/
$object = new class() {
public $start = 10;
public $end = 5;
};
// Validator uses Doctrine Annotation library
AnnotationRegistry::registerAutoloadNamespace(
'Symfony\Component\Validator\Constraints',
$projectRootPath . '/vendor/symfony/symfony/src'
);
$validator = (new ValidatorBuilder())
->enableAnnotationMapping()
->getValidator();
$violationList = $validator->validate($object);
dump($violationList[0]->getMessage());
^ "Why is you start value bigger than the end value?"