Symfony:在提交表单后更新表单字段

3

我很新于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
1
我之前做过这个,收到了这个消息: “您无法更改已提交表单的数据。” - Wolfram
我认为这是因为您的表单已经绑定到实体。表单已经获取了实体的数据,当实体发生更改时不会更新。请使用 $form['field1']->setData( 'value2' ); - Jimish Gamit
如果是这样,最好在else部分重新创建整个表单。else { $myentity->setField1 ('value2'); $form = $this->createForm (TaskType::class,$myentity); } - Jimish Gamit
请问您能否在您的表单中添加这个东西(TaskType)吗? $form...->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) { if(!$event->getForm()->isValid()){ $event->getForm()->get('field1')->setData('value1'); } - Jimish Gamit
显示剩余2条评论
2个回答

1
在您的表单中添加以下内容:TaskType.php
public function buildForm(FormBuilderInterface $builder, array $options){
    //YOUR CODE
    $builder->addEventListener(FormEvents::POST_SUBMIT, 
         function (FormEvent $event) { 
           if(!$event->getForm()->isValid()){
             $event->getForm()->get('field1')->setData('value1'); 
           }
         });

好的,这似乎不是正确的位置。我打算修改我的控制器,而不是我的表单。 尽管如此,我无法运行这段代码: Catchable Fatal Error: Argument 1 passed to AppBundle\Form\Type\TaskType::AppBundle\Form\Type\{closure}() must be an instance of AppBundle\Form\Type\FormEvent, instance of Symfony\Component\Form\FormEvent given - Wolfram
请问您能否在这里粘贴您的 TaskType.php 文件? - Jimish Gamit
这在新版本中有效吗? 我在Symfony6上遇到了这个错误AlreadySubmittedException HTTP 500 Internal Server Error 您无法更改已提交的表单数据。 - SpicyTacos23

-1
<?php
namespace AppBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\OptionsResolver\OptionsResolver;

class TaskType extends AbstractType
{
    public function buildForm (FormBuilderInterface $builder, array $options)
    {
        $builder
          ->add ('field1', TextType::class)
          ->add ('field2', TextType::class)
          ->add ('field3', TextType::class);
    }

    public function configureOptions (OptionsResolver $resolver)
    {
        $resolver->setDefaults (array (
          'data_class' => 'AppBundle\Entity\Task'
        ));
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接