我很新于Symfony,并正在学习。我遇到了以下问题:
在我的控制器类中,我想在提交后更新一些输入字段。'indexAction'函数的代码类似于以下内容:
public function indexAction (Request $request) {
$myentity = new TestEntity();
$myentity->setField1 ('value1');
$form = $this->createForm (TaskType::class,$myentity);
$form->handleRequest ($request);
if ($form->isValid())
return $this->redirectToRoute ('nextPage');
else
{
// and here is my problem. I would like to set Field1 to 'value2',
// but I cannot, as the form is already submitted. Or with the
// following command directly setting the entity, the change is not
// taken into account anymore.
$myentity->setField1 ('value2');
}
return $this->render ('test.html.twig', array (
'form' => $form->createView());
}
如果第一次调用表单,我希望将Field1设置为value1。一旦我点击“提交”按钮(并且表单无效),相同的表单应该出现,但是Field1应该设置为value2。
不知道如何实现上述功能。任何帮助都将不胜感激。 非常感谢, Wolfram
$form->get('field1')->setData('value');
替换$myentity->setField1 ('value2');
? - Jimish Gamit$form['field1']->setData( 'value2' );
。 - Jimish Gamitelse
部分重新创建整个表单。else { $myentity->setField1 ('value2'); $form = $this->createForm (TaskType::class,$myentity); }
- Jimish GamitTaskType
)吗?$form...->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) { if(!$event->getForm()->isValid()){ $event->getForm()->get('field1')->setData('value1'); }
- Jimish Gamit