【问题标题】:Submit two unrelated forms rendered on the same page提交在同一页面上呈现的两个不相关的表单
【发布时间】:2013-08-26 09:09:34
【问题描述】:

有没有办法在同一页面上嵌入两个表单(数据在 2 个不相关的实体上)并仅使用一个提交按钮提交表单?

这个想法是在一个控制器动作中验证两个提交的表单。

Entity1Entity2 完全没有共同点。

                                  |--------------------------
                                  |    Form 1 (Entity 1)    |
                                  |--------------------------
 ---------------                  |
 |  Main Form  |------------------
 ---------------                  |
                                  |--------------------------
                                  |    Form 2 (Entity 2)    |
                                  |--------------------------

有人知道这是否可行吗?

非常感谢。

【问题讨论】:

    标签: symfony


    【解决方案1】:

    这是可能的。像这样的东西应该可以工作:

    $entity1 = new Entity1();
    $entity2 = new Entity2();
    
    $form = $this->createMainForm();
    
    $form->setData(array(
        'entity1' => $entity1,
        'entity2' => $entity2,
    ));
    
    if ($request->isMethod('POST')) {
        $form->bindRequest($request);
        if ($form->isValid()) {
            // $entity1 and $entity2 should contain the post data
            // and can be persisted or whatever it is you want to do
            // ...
    

    您还可以创建一个包含两个实体的模型并为其创建一个表单。使用$mainEntity->getEntity1(); 检索封装的实体。

    【讨论】:

    • 非常感谢@mahok!我想知道 $this->createMainForm 中的表单类型如何?因为它在 2 个不同的实体上嵌入了两种不同的类型...
    • 你只需要像这样添加两个实体:$builder->add('entity1', 'entity', array('class' => ...) 在 MainFormType 中。
    • 但这是一个实体字段类型...它将显示保存在 Entity1 中的所有对象的选择列表...而这并不是我真正想要的。我要做的是同时显示 entity1 的表单并显示 entity2 的表单,而不是 entity1 中保存的对象列表以及 entity2 中保存的对象列表。无论如何,您创建模型的想法似乎是可行的方法。我正在尝试一下,并会相应地接受您的回答:) 非常感谢@mahok。很好的答案很可能会引导我找到可行的解决方案:)
    • 我的错...当然应该是$this->builder->add('entity1', new Entity1Type());。这会将实体的表单嵌入到原始表单中。
    • 啊太棒了!我会试一试,但这看起来绝对正确。太棒了@mahok。
    【解决方案2】:

    Symfony 3.20

    当您构建 FormType 时,在控制器中执行如下所示的路由和操作,使用“ if ($formRegister->isSubmitted() && $formRegister->getClickedButton('form2') &&...)”

    class WelcomeController extends Controller
    {
        /**
         * @Route("/welcome", name="welcome")
         */
    
        public function welcomeAction(Request $request)
        {
    
    
    
            $uLogin = new User();
            $formLogin = $this->createForm(LoginUserFormType::class, $uLogin);
    
    
    
            $uRegister = new User();
            $formRegister = $this->createForm(UserRegistrationFormType::class, $uRegister);
    
    
    
            $authenticationUtils = $this->get('security.authentication_utils');
            $error = $authenticationUtils->getLastAuthenticationError();
            // last username entered by the user
            $lastUsername = $authenticationUtils->getLastUsername();
    
    
    
    
            if ($request->isMethod('post')){
    
                $formLogin->handleRequest($request);
                $formRegister->handleRequest($request);
    
                if($formLogin->isSubmitted() && $formLogin->getClickedButton('form1')){
    
    
                    return $this->redirectToRoute('login_success');
                }
    
                if ($formRegister->isSubmitted()  && $formRegister->getClickedButton('form2') && $formRegister->isValid() ) {
    
    
                    $password = $this->get('security.password_encoder')
                        ->encodePassword($uRegister, $uRegister ->getPlainPassword());
                    $uRegister ->setPassword($password);
    
                    $uRegister->setRole('ROLE_USER');
    
    
                    $em = $this ->getDoctrine() ->getManager();
                    $em -> persist($uRegister);
                    $em -> flush();
    
    
                    return $this->redirectToRoute('register_success');
    
                }
    
    
            }
    
    
    
    
    
    
    
    
    
            return $this->render(
            'form/welcome.html.twig',
                array(
                    'form1' => $formLogin -> createView(),
                    'form2' => $formRegister -> createView(),
                    'last_username' => $lastUsername,
                    'error'         =>  $error,
                )
            );
        }
    

    建立你的路由,就是这样:-)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-20
      • 2017-03-21
      • 1970-01-01
      • 2012-11-29
      相关资源
      最近更新 更多