Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
758 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...