[英]Symfony2.3 - pass variables from controller to formtype and back
I want to pass a variable from controller to formtype and back to controller. 我想将变量从控制器传递到formtype,然后再传递回控制器。 I tried to realize the topics about passing data from controller and passing values but I get the error
我试图实现有关从控制器传递数据和传递值的主题,但出现错误
ContextErrorException: Notice: Undefined variable: id in C:\\xampp\\htdocs\\pso\\src\\Pso\\ProjectBundle\\Form\\ProjectuserType.php line 38
ContextErrorException:注意:未定义的变量:C:\\ xampp \\ htdocs \\ pso \\ src \\ Pso \\ ProjectBundle \\ Form \\ ProjectuserType.php行中的id
and I can't see, where my mistake is. 我看不到我的错误在哪里。 I think I defined the variable.
我想我定义了变量。 Therefore I would be glad for a hint, what I didn't see.
因此,我很高兴得到一个提示,我没有看到。
Controller: 控制器:
$form = $this->createForm(new ProjectuserType($id));
Formtype: Formtype:
class ProjectuserType extends AbstractType
{
private $id;
public function __construct($id)
{
$this->id = $id;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$id = $this->id;
$builder->addEventListener(
FormEvents::PRE_SET_DATA,
function (FormEvent $event) {
$form = $event->getForm();
$formOptions = array(
'class' => 'Pso\LogBundle\Entity\User',
'property' => 'username',
'multiple' => 'true',
'expanded' => 'true',
//Line 38
'query_builder' => function (EntityRepository $er) use($id) {
return $er->createQueryBuilder('x')->FROM('PsoLogBundle:user', 'u');
},
);
$form->add('user', 'entity', $formOptions)
->add('Submit', 'submit', array(
'attr'=> array (
'formnovalidate' => 'formnovalidate'
)
));
}
);
}
After submitting the form, I want to pass the variable back to controller. 提交表单后,我想将变量传递回控制器。
My goal with all that is, that I have the form, which represents all users, that are actually not related to the representated object. 我的目标是拥有一个代表所有用户的表格,该表格实际上与所表示的对象无关。 For that I want to pass the id for the representated object to the form.
为此,我想将代表对象的ID传递给表单。 When I activate a checkbox in the form and submitting it, I want to create an entry in db to relate the user to the object with the id.
当我激活表单中的复选框并提交时,我想在db中创建一个条目以将用户与具有id的对象相关联。
You need to add one more use ($id)
: 您需要再添加一个
use ($id)
:
...
$builder->addEventListener(
FormEvents::PRE_SET_DATA,
function (FormEvent $event) use ($id) {
....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.