I was wondering if there is a way to compare old and new values in a validator within an entity prior to a flush.
I have a Server
entity which renders to a
For the record, here is the way to do it with Symfony5.
First, you need to inject your EntityManagerInterface service in the constructor of your validator. Then, use it to retrieve the original entity.
/** @var EntityManagerInterface */
private $entityManager;
/**
* MyValidator constructor.
* @param EntityManagerInterface $entityManager
*/
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}
/**
* @param string $value
* @param Constraint $constraint
*/
public function validate($value, Constraint $constraint)
{
$originalEntity = $this->entityManager
->getUnitOfWork()
->getOriginalEntityData($this->context->getObject());
// ...
}