duanlingzei0170 2016-04-11 15:14
浏览 17
已采纳

无法使用symfony处理表单

I have controller, which render a form which is rendered to every page.

The problem is, that the form doesn't care about submit event.

Every page extends base.html.twig in which is

{{ render(controller('AppBundle:NewsLetter:formRender')) }}

This is the controller:

<?php

namespace AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

use AppBundle\Entity\Emails;

use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;

class NewsLetterController extends Controller
{

    public function formRenderAction(Request $request)
    {
        $email = new Emails();

        $form = $this->createFormBuilder($email)
            ->add('email', EmailType::class, array('label' => false, 'attr' => array('placeholder' => 'Your e-mail address')))
            ->add('save', SubmitType::class, array('label' => 'SUBMIT!'))
            ->getForm();

        $form->handleRequest($request);

        if ($form->isSubmitted()) {

            die(); //breakpoint

            $em = $this->getDoctrine()->getManager();
            $em->persist($email);
            $em->flush();

            $this->addFlash('success', 'Email was successfully saved.');
        }

      return $this->render('newsLetter.html.twig', array('form' => $form->createView()));
    }
}

And finally newsletter.html.twig

<div class="newsletter">
  <p>Nechte si zasílat články e-mailem</p>
  {{ form_start(form) }}
    {{ form_widget(form) }}
    {{ form_end(form) }}
</div>

So the form is correctly rendered, but nothing happens after submit.

  • 写回答

1条回答 默认 最新

  • dtxooq1020 2016-04-11 15:26
    关注

    When you add a form without specifying the action it will post back to the same url that rendered it. So if you have a form that appears on multiple pages each controller function must be able to handle that form.

    You could either create a base Controller that has that function and extend your other controllers. If you did it this way, each action method would have to call this function and you would have to handle the submit of the form individually as you may have multiple forms on that page.
    E.g $emailAction = $form->get('addEmail')->isClicked()

    Or you could set the action of your form to a dedicated route that would handle just that form, and then maybe redirect back to the original url (maybe pass it as a hidden field, the choice is yours).
    E.g

    $form = $this->createForm(Foo::class, $task, array(
        'action' => $this->generateUrl('target_route'),
        'method' => 'POST',
    ));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?