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',
    ));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作