我正在处理一个表单,并希望将一个数组传递给我的表单,该数组将满足我的所有项目,以便我的表单可以选择包含所有这些项目的选项。
我已经回答了很多答案,但我无法理解这件事。我知道我想做一些像:
$formulaire=$this->createForm(new ModifierSupprimerProjet(), null, $myArray);
但是,我想将我的数组的所有内容添加到getDefaultOptions()... 我这样做?另外一点,假设是createForm方法的第二个参数?
以下是最接近解决问题的帖子:
答案 0 :(得分:0)
使用项目实体填充选择表单元素的最佳方法是使用entity form field。因此,您不必将任何选项传递给表单类。
use strict;
sub main {
open F, 'input.txt' or die $!;
my @pairs;
while (<F>) {
my @fields = split(/\|/);
my $key = [ @fields[0, 1, -2] ];
push @pairs, [$key, $_];
}
close F;
my @sorted_pairs = sort {
my $a_key = $a->[0];
my $b_key = $b->[0];
$a_key->[0] <=> $b_key->[0]
|| $a_key->[1] cmp $b_key->[1]
|| $a_key->[2] cmp $b_key->[2]
} @pairs;
foreach my $pair (@sorted_pairs) {
print $pair->[1];
}
}
main;
如果您需要填充choice field,还有另一种最佳做法。您应该将表单类型类定义为服务,并将Entity Manager注入其中。因此,您可以从数据库中获取任何数据并填充任何选择字段。
的services.xml
$builder
->add('project', 'entity', [
'label' => 'form.project',
'class' => 'AppBundle\Entity\Project',
'property' => 'name',
'required' => true,
'empty_value' => 'form.select_empty_value',
'query_builder' => function($repository) {
return $repository->createQueryBuilder('p')->add('orderBy', 'p.name ASC');
},
])
;
ModifierSupprimerProjet.php
<service id="app.form.modifier_supprimer_projet" class="AdminBundle\Form\ModifierSupprimerProjet">
<tag name="form.type" alias="app_modifier_supprimer_projet" />
<argument type="service" id="doctrine.orm.entity_manager"/>
</service>
控制器
<?php
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Doctrine\ORM\EntityManager;
class ModifierSupprimerProjet extends AbstractType
{
/**
* @var EntityManager
*/
protected $em;
/**
* @param EntityManager $em
*/
public function __construct(EntityManager $em)
{
$this->em = $em;
}
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('project', 'choice', [
'label' => 'form.project',
'choices' => $this->em->getRepository('AppBundle:Project')->getProjectsList(),
])
;
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults([
'data_class' => 'AppBundle\Entity\ModifierSupprimerProjet',
]);
}
/**
* @return string
*/
public function getName()
{
return 'app_modifier_supprimer_projet';
}
}
如果您的表单映射到任何实体,则createForm方法的第二个参数是Entity。
答案 1 :(得分:0)
前段时间我不得不做类似的事情,我必须得到图标列表并将其传递给表格,这就是我实现它的方式。
在我的行动中
// my entity
$services = new Services();
$icons = the query for icons, in your case the projects
// creating form view, notice 'icons' being passed to the form
$form = $this->createForm(new AddServices(), $services, array('icons' => $icons));
在表单类
中namespace Acme\YourBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class AddServices extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
//build your form here
$builder->add('....');
//form field linked with your dropdown options, in your case the projects
$builder->add('icon', 'choice', array(
'choices' => $options['icons'],
'required' => false,
));
}
public function getName()
{
return 'addServices';
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
//notice icons being set here as default options
$resolver->setDefaults(array(
'icons' => null,
));
}
}
然后在你的树枝里面
{{ form_widget(form.icon, { 'attr': {'class': ''} }) }}
我相信可能还有其他方法可以实现这一目标,但这就是我如何做到这一点,似乎工作得很好
答案 2 :(得分:0)
我们假设您已经阅读了documentation,但无法理解如何使用它..它非常简单,表单类型有3种重要方法, buildForm
用于构建表单的字段,setDefaultOptions
用于版本&lt; = 2.6或configureOptions
用于版本&gt; 2.6,其中您指定buildForm
方法使用的选项(由表单组件在内部,getName
- 表单的唯一名称(here is对此方法的简单说明)。
如果我们回到你的问题,你可以解决这个问题:
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver
->setDefaults([
'data_class' => 'YOUR_ENTITY' /* FormComponent autmatically maps the form fields with your entity if you set this option and pass an object as an 2-arguement to `createForm` method in your controller */
'my_custom_option' => null /* here we specify our custom option and set its initial value as null */
])
->setRequired([ /* this is optional, which tells the 'my_custom_option' option is required */
'my_custom_option'
])
->setAllowedTypes([ /* this one is also optional, which specify the type your 'my_custom_option' option */
'my_custom_option' => 'Symfony\Component\HttpFoundation\Request' // this is an example, which allows only `request` object
])
;
}
public function buildForm(...)
{
$myCustomOption = $options['my_custom_option']; // here we get our 'my_custom_option'
....
}
在您的控制器中:
$formulaire=$this->createForm(new ModifierSupprimerProjet(), OBJECT_OR_NULL /* here we set our object which we've already assigned to `data_class` or we set to NULL */, [
'my_custom_option' => $request
]);