Class 特征约束不起作用
Class constraint in trait not working
我有一个简单的实体类型 "City"
class City
{
use GeoTrait;
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="title", type="string", length=255)
*/
private $title;
...
}
如您所见,它使用了一个 Trait,看起来像这样
use AppBundle\Validator\Constraint as AppConstraint;
/**
* @AppConstraint\IsDuplicateLocation
* @AppConstraint\IsLocation
*/
trait GeoTrait
{
/**
* @var string
*
* @ORM\Column(name="geoLat", type="decimal", nullable = true, precision=9, scale=6)
*/
private $geoLat;
/**
* @var string
*
* @ORM\Column(name="geoLng", type="decimal", nullable = true, precision=9, scale=6)
*/
private $geoLng;
}
现在,如果我创建一个新实体并尝试使用验证器服务验证它,它似乎无法验证我的自定义 class 约束。
如果我向特征添加字段约束,验证工作正常,但不适用于 class 约束。为什么这不起作用?
特征不是 class,因此您不能在自定义约束 "getTargets()" 方法中将其作为目标。
尝试将您的 class 约束添加到城市 class
/**
* @AppConstraint\IsDuplicateLocation
* @AppConstraint\IsLocation
*/
class City
{
...
}
我有一个简单的实体类型 "City"
class City
{
use GeoTrait;
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="title", type="string", length=255)
*/
private $title;
...
}
如您所见,它使用了一个 Trait,看起来像这样
use AppBundle\Validator\Constraint as AppConstraint;
/**
* @AppConstraint\IsDuplicateLocation
* @AppConstraint\IsLocation
*/
trait GeoTrait
{
/**
* @var string
*
* @ORM\Column(name="geoLat", type="decimal", nullable = true, precision=9, scale=6)
*/
private $geoLat;
/**
* @var string
*
* @ORM\Column(name="geoLng", type="decimal", nullable = true, precision=9, scale=6)
*/
private $geoLng;
}
现在,如果我创建一个新实体并尝试使用验证器服务验证它,它似乎无法验证我的自定义 class 约束。
如果我向特征添加字段约束,验证工作正常,但不适用于 class 约束。为什么这不起作用?
特征不是 class,因此您不能在自定义约束 "getTargets()" 方法中将其作为目标。
尝试将您的 class 约束添加到城市 class
/**
* @AppConstraint\IsDuplicateLocation
* @AppConstraint\IsLocation
*/
class City
{
...
}