2014-12-03 127 views
2

我嘗試在同一頁面中創建一個簡單的登錄註冊表。登錄一種表單登錄的一種形式。我正在使用symfony(2.6)Symfony 2在同一頁面中的多個表單Submition

我設法在頁面中呈現2個表單,但如果我嘗試提交註冊表單,則會向我發送其他表單不能爲emty的消息。

之前,我試圖合併我註冊工作沒有任何proble的網頁我登錄不(剛創建的形式),我的代碼:

是渲染到模板

AccountController.php文件。表單創建已RegistrationType.php和LoginType.php

<?php 
namespace Spyros\MapBundle\Controller; 

use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
use Spyros\MapBundle\Form\Type\RegistrationType; 
use Spyros\MapBundle\Form\Type\LoginType; 
use Spyros\MapBundle\Form\Model\Registration; 
use Spyros\MapBundle\Form\Model\Login; 
use Symfony\Component\HttpFoundation\Request; 
use Symfony\Component\HttpFoundation\Response; 


class AccountController extends Controller 
{ 
    public function registerAction() 
    { 
     $registration = new Registration(); 
     $form1 = $this->createForm(new RegistrationType(), $registration, array(
      'action' => $this->generateUrl('account_create'), 
     )); 

     $login = new Login(); 
     $form2 = $this->createForm(new LoginType(), $login, array(
      'action' => $this->generateUrl('account_create'), 
     )); 

     return $this->render(
      'SpyrosMapBundle:Account:register.html.twig', 
      array('form1' => $form1->createView(), 
        'form2' => $form2->createView()) 
     ); 


    } 







    public function createAction(Request $request) 
    { 


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

           $form1 = $this->createForm(new RegistrationType(), new Registration()); 

           $form1>handleRequest($request); 

           if ($form1->isValid()) { 
            $registration = $form1->getData(); 

            $em->persist($registration->getUser()); 
            $em->flush(); 

              echo "<p>hi</p>"; 
           } 
    return $this->render(
      'SpyrosMapBundle:Account:register.html.twig', 
      array('form1' => $form1->createView()) 
     ); 



    } 

    } 

register.html.twig實施

{% extends 'SpyrosMapBundle::rightform.html.twig' %} 
{% form_theme form1 'bootstrap_3_layout.html.twig' %} 
{% form_theme form2 'bootstrap_3_layout.html.twig' %} 
{% block body %} 
<body> 
<section class="container"> 

    <section class="row"> 

     <section class="col col-lg-9"> 
     {{ form_start(form2 , {'attr': {'class': 'form-inline'}})}} 
     {{ form_row(form2.user.email)}} 
     {{ form_row(form2.user.plainPassword)}} 
     <div id="inline"> 
     {{ form_row(form2.Login) }} 

     </div> 
     </section> 

     <section class="col col-lg-3"> 

     {{ form(form1) }} 

     </section> 

    </section> 


</section> 

</body> 

{% endblock %} 

我查看網頁:

my view page

如何提交我的註冊表格(form1)沒有form2給我一個信息來填補它的領域。

+0

不要使用相同的動作=「」每種形式,但要爲每個表單專用的控制器,並確保沒有試圖處理其他的。 – Erik 2014-12-03 12:21:55

+0

如果我爲每個表格製作2個專用控制器,我將如何將它們租賃給同一頁面? – 2014-12-03 12:31:47

+0

你是什麼意思?你希望能夠同時填寫和發送兩份表格嗎? – Erik 2014-12-03 12:35:13

回答

3

我假設它是你的瀏覽器抱怨空白字段?關於Symfony 2格式的一些不太有趣的事情是,默認情況下,所有字段都是必需的。這是通過在輸入元素上設置一個必需的屬性來實現的。瀏覽器然後處理該屬性。

幾種方法來解決這個問題。創建該元素時,需要設置= FALSE:

 $builder->add('name','text', array(
     'required' => false, /***** Make it optional *****/ 
     'label' => 'Your Name', 
     'trim'  => true, 
     'constraints' => array(
      new NotBlankConstraint($constraintOptions), 
     ), 
     'attr' => array('size' => 30), 
    )); 

創建表單時,那麼一切都默認爲可選,您也可以在它傳遞給你的表格選項。

您還可以在您的樹枝模板中爲表單元素本身添加一個簡單的novalidate屬性。

順便說一句,正如@Spiro所建議的,如果您使用內置的安全系統,那麼您將需要將每個表單提交到不同的URL。否則,安全系統將攔截您的創建用戶請求。

http://symfony.com/doc/current/reference/forms/types/form.html#required

+0

thx很多我的朋友:)嘗試第一次工作,但我會做安全系統的secont警戒 – 2014-12-03 17:50:45

相關問題