I have two entities: Company
and Location
. One company has one location (while one location may "have" multiple companies). Now when a user creates a company, I want him/her to able able, to create the location in the same form. So I use the following
$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',
])
This works fine in creating the form. Now it comes to validation. I added @Assert
annotations for both entities in their respective files. While company
validation works, location
does not get automatically validated.
I managed to get validation by adding constraint
properties to the new $builder->create('location')
elements, but this means duplicated code (once in Entity and at least once in every form that needs location
).
How can I solve it so the form gets validated by using the entity's annotation?