php Symfony 2 - 如何将数据传递给 formBuilder?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6716776/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Symfony 2 - how to pass data to formBuilder?
提问by jacobmaster
I'm using entity choice list in my form. I want to use only specific entities (in example: only groups that user belongs to)
So, in controller, I'm getting these groups, and trying to pass them into formBuider
.
我在表单中使用实体选择列表。我只想使用特定的实体(例如:仅用户所属的组)因此,在控制器中,我正在获取这些组,并尝试将它们传递到formBuider
.
Controller:
控制器:
/.../
$groups = $em->getRepository('VendorMyBundle:Group')->getUserGroups($user);
$form = $this->createForm(new Message($groups), $message);
/.../
so, what now? how to use it in formBuilder? how to change this line to use passed array of groups?
所以现在怎么办?如何在 formBuilder 中使用它?如何更改此行以使用传递的组数组?
->add('group','entity',array('class' => 'Vendor\MyBundle\Entity\Group', 'label'=>'Group:'))
or in the other way:
或以其他方式:
class MessageType
{
/.../
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('group','entity',
array(
'class' => 'Vendor\MyBundle\Entity\Group',
'property' => 'name',
'query_builder' => function ($repository) {
$qb = $repository->createQueryBuilder('group');
$qb->add('where', 'group.administrator = :user');
$qb->setParameter('user', $user->getId());
return $qb;
},
'label' => 'Group'
)
)
// Continue adding fields
;
}
/.../
}
so how can i get object $user to use in form builder? ($user represent current logged user)
那么我怎样才能让对象 $user 在表单构建器中使用呢?($user 代表当前登录的用户)
回答by Bacteries
You can give the object you want to use in the __construct() method.
你可以在 __construct() 方法中给出你想要使用的对象。
Eg :
例如:
$form = $this
->get('form.factory')
->create(new ApplyStepOneFormType($this->company, $this->ad), $applicant);
In your form type :
在您的表单中输入:
function __construct(\Your\Bundle\Entity\Company $company, \DYB\ConnectBundle\Entity\Ad $ad) {
$this->company = $company;
$this->ad = $ad;
}
And then in your form type in buildForm method :
然后在你的表单中输入 buildForm 方法:
$company = $this->company;
$builder->add('ad', 'entity', array(
'class' => '\Your\Bundle\Entity\Ad',
'query_builder' => function(\Your\Bundle\Repository\AdRepository $er) use ($company) {
return $er->getActiveAdsQueryBuilder($company);
},
));
回答by user3335780
//In controller pass the value which you want to use in builder form in array like
$object = new Question();
$form->create(new QuestionType() , $object , array('sqtname'=>2,'question_type'=>2));
//In Form type class
public function buildForm(FormBuilderInterface $builder , array $options)
{
//for setting data field dynamically
if (array_key_exists('question_type', $options) && $options['question_type'] != '') {
$data = $em->getReference("RecrutOnlineStandardBundle:StdQuestionType",$options['question_type']->getId());
} else {
$data = "";
}
$builder->add('StdQuestionType', 'entity', array(
'class' => 'TestStandardBundle:StdQuestionType',
'property' => 'name',
'empty_value' => 'Sélectionner un question type',
'required' => true,
'data' => $data,
'query_builder' => function(EntityRepository $er ) use ( $options ) {
if (isset($options['sqtname']) && $options['sqtname'] != '') {
return $er->createQueryBuilder('sqt')
->where("sqt.name!= ".$options['sqtname']);
} else{
return $er->createQueryBuilder('sqt');
}
}
));
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Test\QuestionBundle\Entity\Question',
'required' => false,
'sqtname' => '',
'question_type' =>''
));
}
回答by Bohdan Yurov
Bacteries' solution IS NOT a good one. For example, if you declare your type as service, it is impossible to pass an object to constructor.
细菌的解决方案不是一个好的解决方案。例如,如果您将类型声明为服务,则不可能将对象传递给构造函数。
A perfect solution is options - just pass data as option to form builder.
一个完美的解决方案是选项 - 只需将数据作为选项传递给表单构建器。
回答by FGREZE
Bacteries' solution is a real good one. Just a note to save headache to other guy like me :)
Bacteries 的解决方案是一个非常好的解决方案。只是为了避免像我这样的其他人头痛:)
In this part may I point out the use ($company)
part.
It was hidden by the frame and of course nothing works properly without it.
在这部分,我可以指出这一use ($company)
部分。它被框架隐藏了,当然没有它就无法正常工作。
$builder->add('ad', 'entity', array(
'class' =>
'\Your\Bundle\Entity\Ad',
'query_builder' =>
function(\Your\Bundle\Repository\AdRepository $er) use ($company) {
return $er->getActiveAdsQueryBuilder($company);
},
)
);
回答by Ondrej Slinták
If you want to use custom query, you have to set query_builder
option as follows:
如果要使用自定义查询,则必须query_builder
按如下方式设置选项:
use Doctrine\ORM\EntityRepository;
...
$message = new Message();
$form = $this->createFormBuilder($message)
->add('group', 'entity', array(
'class' => 'Vendor\MyBundle\Entity\Group',
'label'=>'Group:',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('g')
->... // whatever you want to do
}
))
->getForm();
You can find more info about query builder in Doctrine manualand about options for entity
in Symfony2 manual.
您可以找到有关查询生成器的详细信息在教义手册为和关于选择entity
在Symfony2的手工。
回答by user1954544
Best way (my opinion) is give to your form entityManager and select all you need in it. But don't forget to declare empty key in setDefaults() otherwise data won't pass to your builder.
最好的方法(我的意见)是给你的表单 entityManager 并选择你需要的所有内容。但是不要忘记在 setDefaults() 中声明空键,否则数据不会传递给您的构建器。
Something like this one
像这样的东西
public function buildForm(FormBuilderInterface $builder, array $options)
{
$options['em']->getRepository(''); // select all you need
$builder->add('title', 'text')
->add('content', 'textarea');
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Main\BlogBundle\Entity\Post',
'validation_groups' => array('post'),
'required' => false,
'em' => null // this var is for your entityManager
));
}
Apply EM as simple option...
应用 EM 作为简单选项...