我有两个实体:Company
和Location
。一家公司有一个位置(而一个位置可能"有#34;多家公司)。现在,当用户创建公司时,我希望他/她能够以相同的形式创建位置。所以我使用以下
$builder
->add('name', TextType::class, ['label' => 'company.name'])
->add('size', IntegerType::class, ['label' => 'company.size'])
->add( $builder->create('location', FormType::class, [
'label' => 'company.location',
'by_reference' => true,
'data_class' => 'AppBundle\Entity\Location',
])
->add('street', TextType::class, [
'label' => 'location.street',
])
->add('number', TextType::class, [
'label' => 'location.number',
])
这适用于创建表单。现在谈到验证。我在各自的文件中为两个实体添加了@Assert
注释。 company
验证有效,location
无法自动验证。
我设法通过向新的constraint
元素添加$builder->create('location')
属性来获得验证,但这意味着重复的代码(一次在实体中,并且在每个需要location
的表单中至少一次)
我如何解决它,以便通过使用实体的注释来验证表单?
答案 0 :(得分:3)
默认情况下,验证不会遍历对象或集合的属性。使用valid constraint:
http://symfony.com/doc/current/reference/constraints/Valid.html
您也可以为集合设置遍历选项。