为Collection Type创建数据数组

时间:2017-06-26 22:40:50

标签: php forms symfony model-view-controller fosuserbundle

我正在开发一个基于Symfony的项目,我正在使用FosUserBundle。

我遇到问题,在尝试渲染CollectionType:Class时,我想让我的用户在我的数据库中填充我尚未拥有的内容。

从我在Symfony文档中读到的内容来看,为了使该类型工作,有必要将一组数据Pre Set作为条目。

所以我的问题是,“无论如何,我是否有创建数据数组并将其传递到我的字段以使其显示?”

我的工作室类型:

class WorkshopType extends AbstractType implements ContainerAwareInterface
{
    use ContainerAwareTrait;
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add( 'title', TextType::class, array('label'=>"Titre"))
            ->add( 'goal', TextareaType::class, array('label'=>"But"))
            ->add( 'theme', TextType::class, array('label'=>"Theme"))
            ->add( 'target', TextType::class, array('label'=>"Cible"))
            ->add( 'summary', TextareaType::class, array('label'=>"Résumé"))
            ->add('maxParticipants', NumberType::class, array('label'=>"Nombre de Participants Maximum", 'data' => 0))
            ->add('moderatorsNumber', NumberType::class, array('label'=>"Nombre de Modérateurs",'data' => 0))
            ->add('materials', CollectionType::class, [
                 //'label' => 'Matériaux',
                 'entry_type' => TextType::class,
                'entry_options'  => array(
                    'attr'      => array('class' => 'material-box')
                ),
                'prototype' => true,
                 'allow_add' => true,
                 'allow_delete' => true,
             ])
            ->add( 'link', UrlType::class, array('label'=>"Lien"))
            ->add('video', FileType::class, array('label'=>"Ajouter une vidéo", "data_class" => null))
            ->add('image', FileType::class, array('label'=>"Ajouter une image", "data_class" => null))
            ->add('document', FileType::class, array('label'=>"Ajouter un document", "data_class" => null))
            ->add('tags', EntityType::class, [
                'class' => Tag::class,
                'choice_label' => 'name',
                'multiple' => true,
                'expanded' => true,
                'empty_data' => 'Aucun',
                'label' => 'Ajouter des affinités',
            ])
            ->add('duration', TimeType::class, array(
                'label' => 'Durée',
                'input'  => 'timestamp',
                'widget' => 'choice',
            ))
            ->add('preparationTime', TimeType::class, array(
                'label' => 'Temps de préparation',
                'input'  => 'timestamp',
                'widget' => 'choice',
            ))
        ;

        // It's here you have to make changes if you want to change the duration render type
        $builder
            ->get('duration')->addModelTransformer(new SecondDurationDataTransformer());
        $builder
            ->get('preparationTime')->addModelTransformer(new SecondDurationDataTransformer());
        $builder
            ->get('document')->addModelTransformer(new MediaTransformer(
                "ApiBundle\Entity\Workshop",
                $options['entityId'],
                $this->container->get('doctrine.orm.entity_manager'),
                $this->container->get('bnpp.api.file_manager.local'),
                "document",
                "ApiBundle\Entity\Document"));
        $builder
            ->get('image')->addModelTransformer(new MediaTransformer(
                "ApiBundle\Entity\Workshop",
                $options['entityId'],
                $this->container->get('doctrine.orm.entity_manager'),
                $this->container->get('bnpp.api.file_manager.local'),
                "image",
                "ApiBundle\Entity\Image"));
        $builder
            ->get('video')->addModelTransformer(new MediaTransformer(
                "ApiBundle\Entity\Workshop",
                $options['entityId'],
                $this->container->get('doctrine.orm.entity_manager'),
                $this->container->get('bnpp.api.file_manager.local'),
                "video",
                "ApiBundle\Entity\Video"));
    }

    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Workshop::class,
            'required' => false,
            "entityId" => null,
        ]);
    }

    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix()
    {
        return 'apibundle_workshop';
    }


}

谢谢大家,

1 个答案:

答案 0 :(得分:0)

您可以将数组作为默认值传递:

'choices' => $yourArray,