如何在Symfony2中将数据设置为CollectionType?

时间:2016-06-22 22:49:32

标签: php symfony

我有两种形式:MainForm和SecondForm。 在第二种形式发出两个字段。如何将数据设置为Second Form? 我试图使用CallbackTransformer,试图使用EventListener,所有不工作。请帮忙举例。

<?php

namespace True\AdminBundle\Form\Type;

use True\KernelBundle\Entity\LandingSection;
use True\KernelBundle\Entity\LandingValue;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\CallbackTransformer;
use Doctrine\Common\Persistence\ObjectManager;
use True\KernelBundle\Form\DataTransformer\LandingTransformer;
use Symfony\Component\Form\Extension\Core\Type\UrlType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Vich\UploaderBundle\Form\Type\VichFileType;
use Vich\UploaderBundle\Form\Type\VichImageType;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;

class FirstFormType extends AbstractType
{
    protected $em;
    private $manager;

    public function __construct(ObjectManager $manager)
    {
        $this->manager = $manager;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $this->em = $options['em'];
       $builder->add($key, CollectionType::class,[
            'type' => SecondFormType::class,
            'allow_add' => true,
            'entry_options' => [
            //    'fields' => $fields,
            ],

            'mapped' => true,
            'prototype' => true,
                'allow_delete' => true,
        ]);

        $builder->get($key)
            ->addModelTransformer(new CallbackTransformer(
                function ($tagsAsArray) {
                   $result = [];
                   $result[] = ['name' => 1, 'name2' => 2];
                   return $result;
                },
                function ($tagsAsString) {
                    return $result;
                }
            ))
        ;

        $builder
            ->add('submit', SubmitType::class, [
                'label' => 'Save',
            ])
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            //'data_class' => LandingValue::class,
            'fields' => null,
            'descriptions' => null,
            'em' => null,
            'template' => null,
            'sectionName' => null,
            'doctrine' => null,
            'websiteId' => null,
            'field_count' => null,
        ));
    }

    public function getBlockPrefix()
    {
        return 'backend_landing_form';
    }
}

第二种形式有两个字段

<?php

namespace True\AdminBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\Form\Extension\Core\Type\UrlType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\CallbackTransformer;
use True\KernelBundle\Entity\LandingValue;

class SecondFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name');
        $builder->add('name2');
    }

}

0 个答案:

没有答案