为集合类型创建数据数组
Create Array of data for Collection Type
我正在做一个基于 Symfony 的项目,我正在使用 FosUserBundle。
我在尝试呈现 CollectionType:Class
时遇到问题,好吧,我想让我的用户用我的数据库中还没有的内容填充字段。
根据我在 Symfony 文档中阅读的内容,为了使该类型工作,有必要将一组数据预先设置为条目。
所以我的问题是,"Is there, anyway for me to create an array of data and pass it to my field to make it appear ?"
我的工作室类型:
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';
}
}
谢谢大家,
您可以像这样将数组作为默认值传递:
'choices' => $yourArray,
我正在做一个基于 Symfony 的项目,我正在使用 FosUserBundle。
我在尝试呈现 CollectionType:Class
时遇到问题,好吧,我想让我的用户用我的数据库中还没有的内容填充字段。
根据我在 Symfony 文档中阅读的内容,为了使该类型工作,有必要将一组数据预先设置为条目。
所以我的问题是,"Is there, anyway for me to create an array of data and pass it to my field to make it appear ?"
我的工作室类型:
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';
}
}
谢谢大家,
您可以像这样将数组作为默认值传递:
'choices' => $yourArray,