Symfony Constraint / EqualTo与实体属性进行比较

时间:2017-04-27 13:53:46

标签: symfony

如何使用\ Symfony \ Component \ Validator \ Constraints \ EqualTo比较实体的两个不同属性以检查它们是否相等?

1 个答案:

答案 0 :(得分:1)

您可以使用Expression validator检查实体的两个不同属性是否相等:

// src/AppBundle/Model/MyEntity.php
namespace AppBundle\Model;

use Symfony\Component\Validator\Constraints as Assert;

/**
 * @Assert\Expression(
 *     "this.getFirstProperty() == this.getSecondProperty()",
 *     message="The first and second properties must be equal!"
 * )
 */
class MyEntity
{
    private firstProperty;

    private secondProperty;

    public function getFirstProperty()
    {
        return $this->firstProperty;
    }

    public function getSecondProperty()
    {
        return $this->secondProperty;
    }
}

我认为\ Symfony \ Component \ Validator \ Constraints \ EqualTo不可能。