phpsymfonyformbuilder

Symfony - retreive unmapped fields in nested forms


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?


Solution

  • 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.