验证“非空” ManyToMany实体

时间:2018-12-21 05:00:44

标签: php symfony doctrine-orm

我要将Entity的ManyToMany成员设为“不为空”

所以我尝试了两种模式。

1)添加@Assert \ NotBlank()

/**
 * Many Users have Many Groups.
 * @Assert\NotBlank()
 * @ORM\ManyToMany(targetEntity="PlaceCat", inversedBy="placeinfos")
 * @ORM\JoinTable(name="placeinfos_placecats")
 */
private $placeCats;

2)在FormObject中添加required => true

$formMapper
    ->add('placeCats',null,array('required' => true)

但是,两者都将被忽略。

是否可以为ManyToMany实体设置'not null'限制?

1 个答案:

答案 0 :(得分:0)

ManyToMany(不为空)表示您至少要1个?然后您需要另一个断言:

/**
 * @Assert\Count(
 *      min = "1",
 *      minMessage = "You have to select at least 1 item"
 * )
 */