Symfony2 Form Validator - Comparing old and new values before flush

前端 未结 4 585
暗喜
暗喜 2021-02-05 10:40

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

4条回答
  •  时光取名叫无心
    2021-02-05 11:04

    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());
    
        // ...
    }
    

提交回复
热议问题