简体   繁体   English

Symfony 2 同一页面中的多个表单提交

[英]Symfony 2 multiple form in same page Submition

I try to make a simple login-register on the same page.我尝试在同一页面上进行简单的登录注册。

  • One form for register一种登记表
  • One form for login.一种登录表单。

I manage to render 2 forms on the page that I want, but if I try to submit the registration form it gives me a message that the other form cannot be empty.我设法在我想要的页面上呈现 2 个表单,但是如果我尝试提交注册表,它会给我一条消息,另一个表单不能为空。

Before I tried to merge the pages my registrations worked without any problem my login not(just created the form) my code:在我尝试合并页面之前,我的注册没有任何问题,我的登录没有(只是创建了表单)我的代码:

AccountController.php the file that renders to the template. AccountController.php呈现给模板的文件。

The form creation has implemented in RegistrationType.php and in LoginType.php表单创建已在RegistrationType.phpLoginType.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: 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:我的查看页面:

我的查看页面

How can i submit my registration form(form1) without form2 give me a message to fill its fields.我如何在没有 form2 的情况下提交我的注册表单(form1)给我一条消息来填写其字段。

I am assuming that it is your browser that is complaining about blank fields?我假设是您的浏览器抱怨空白字段? One of the less amusing things about Symfony 2 forms is that, by default, all fields are required. Symfony 2 表单不那么有趣的事情之一是,默认情况下,所有字段都是必需的。 This is implemented by setting a required attribute on the input element itself.这是通过在 input 元素本身上设置一个 required 属性来实现的。 The browser then process the attribute.然后浏览器处理该属性。

Couple of ways to get around this.解决这个问题的几种方法。 Set required = false when creating the element:创建元素时设置 required = 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),
    ));

You can also pass this in to your form options when creating the form so everything is optional by default.您还可以在创建表单时将其传递给表单选项,因此默认情况下所有内容都是可选的。

You can also add a simple novalidate attribute to the form element itself in your twig template.您还可以在树枝模板中向表单元素本身添加一个简单的 novalidate 属性。

By the way, as @Spiro suggested, if you are using the built in security system then you will need to submit each form to a different url.顺便说一句,正如@Spiro 所建议的,如果您使用的是内置安全系统,那么您需要将每个表单提交到不同的 url。 Otherwise the security system will intercept your create user requests.否则安全系统将拦截您的创建用户请求。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM