【问题标题】:How can I clear form values after successful form submission表单提交成功后如何清除表单值
【发布时间】:2014-06-15 13:01:33
【问题描述】:

表单提交成功后如何清除表单值?

这些没有帮助:

控制器:

namespace Car\BrandBundle\Controller;

use Car\BrandBundle\Entity\BrandEntity;
use Car\BrandBundle\Form\Type\BrandType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class BrandController extends Controller
{
    public function indexAction()
    {
        $form = $this->getFrom();

        return $this->render('CarBrandBundle:Default:brand.html.twig',
                array('page' => 'Brand', 'form' => $form->createView(), 'brands' => $this->getBrands()));
    }

    public function createAction(Request $request)
    {
        if ($request->getMethod() != 'POST')
        {
            return new Response('Only POST method is allowed');
        }

        $form = $this->getFrom();

        $form->handleRequest($request);

        if ($form->isValid())
        {
            $submission = $form->getData();

            $em = $this->getDoctrine()->getManager();

            $brand = new BrandEntity();
            $brand->setName($submission->getName());

            $em->persist($brand);
            $em->flush();

            $this->redirect($this->generateUrl('brand'));
        }

        return $this->render('CarBrandBundle:Default:brand.html.twig',
                array('page' => 'Brand', 'form' => $form->createView(), 'brands' => $this->getBrands()));
    }

    private function getFrom()
    {
        return $this->createForm(new BrandType(), new BrandEntity(),
                array('action' => $this->generateUrl('brandCreate')));
    }

    private function getBrands()
    {
        $repo = $this->getDoctrine()->getRepository('CarBrandBundle:BrandEntity');
        $brands = $repo->findAll();

        return $brands;
    }
} 

【问题讨论】:

    标签: symfony


    【解决方案1】:

    您只需取消设置表单和实体对象。然后,创建新的、干净的实例,以便它们在您的模板中可用。 就我个人而言,我更喜欢仅在正确验证表单时才这样做。

    if($form->isValid()){
    
      // persisting and flushing the entity
    
      unset($entity);
      unset($form);
      $entity = new Entity();
      $form = $this->createForm(new EntityType(), $entity);
    }
    

    为我工作。 干杯。

    【讨论】:

    • 也适用于我 (3.2)。谢谢!
    • 它工作正常,只是注释说没有必要执行unset。
    • 这个没必要,只需要返回redirect的返回值:return $this->redirect($this->generateUrl('brand'));
    【解决方案2】:

    在同一页面处理提交时重置 symfony 表单

    在 Bazyl 的方法中,在取消设置当前表单后创建另一个表单可能是不必要的任务。我建议重定向到同一页面,因为 symfony 文档 (handling-form-submissions) 还显示了一个重定向到另一个控制器的示例。

        if ($form->isSubmitted() && $form->isValid()) {
            // ... perform some action, such as saving the task to the database
            return $this->redirect($request->getUri());
        }
    

    我已将示例源添加到GitHub

    【讨论】:

    • 完美地完成了这项工作,取消设置表单和实体对我不起作用。
    • @tokam 你使用的 symfony 版本是什么?输出是什么?
    • @tokam 我在 Symfony 3.1 中进行了测试并且运行良好。在这里找到我的资源github.com/lahiiru/SymfonyFormResetting
    • return $this->redirectToRoute('route_name');
    • 2 年后,这个答案应该被接受为好的答案。 unset(variable) 然后重新创建更干净
    【解决方案3】:

    不确定是否相关,但当您的表单有效时,您可以使用$this->redirect($this->generateUrl('brand'));。问题是 $this->redirect() 只是创建了一个重定向响应,您的控制器必须返回该响应才能将其考虑在内。换句话说,只需在控制器中间执行 $this->redirect() 就什么都不做(除了实例化一个 RedirectResponse 被 PHP 收集的垃圾)。

    【讨论】:

      【解决方案4】:

      如果使用树枝模板,你可以在那里清除它。 示例:

      {{ form_start(form) }}
          {{ form_widget(form.yourFieldNameHere, { 'value': '' }) }}
      {{ form_end(form) }}
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-22
        • 1970-01-01
        • 1970-01-01
        • 2020-07-17
        • 1970-01-01
        • 2019-06-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多