Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
762 views
in Technique[技术] by (71.8m points)

php - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...