Reputation: 2315
I'm having a Symfony Type ItemType
which is based on an Entity.
class IpQuoteItemsType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('itemName', TextType::class, [
'label' => 'Produktname'
])
...
->add('specialDiscount', PercentType::class, [
'required' => false,
'label' => 'Sonderrabatt',
'mapped' => false,
'attr' => [
'placeholder' => 'Sonderrabatt 0,00 %'
]
])
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => IpQuoteItems::class
));
}
}
Which is used as a CollectionType in the final form:
class IpQuotesType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
...
$builder->add('products', CollectionType::class, [
'entry_type' => IpQuoteItemsType::class,
'data' => $items
]);
}
}
Under no circumstances I receive the unmapped field specialDiscount
. It is still available in the PRE_SUBMIT event of the ItemsType
but can't be found anywhere in the final form QuotesType
.
Is it possible to sumit unmapped data in nested forms?
Upvotes: 2
Views: 1322
Reputation: 111
Also a thank you to @threeside for his solution. I also use a collection field to embed a subform. In my case it is a FileType field to upload images which have to be unmapped for the processing.
With
$subform_data = $form->get('name_of_collection_field')
you get the form data (or is it a symfony form object?) of the subform. In case of a collection field you can iterate over the items with
foreach($subform_data as $subform_item) {
$items[] = $subform_item->get('unmappedFieldName')->getData();
}
to collect the items in the $items array. The entire trick is to use the form data/object, from that you can extract unmapped fields.
Upvotes: 1
Reputation: 358
You can get unmapped field in your controller like this :
$form->get('nestedEntity')->get('fieldName')->getData()
I don't test with collection but it's work with a customType in OneToOne relation.
Hope this help.
Upvotes: 3