I try to make a simple login-register on the same page.
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.
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.
The form creation has implemented in RegistrationType.php
and in 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:
How can i submit my registration form(form1) without form2 give me a message to fill its fields.
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. This is implemented by setting a required attribute on the input element itself. The browser then process the attribute.
Couple of ways to get around this. Set required = false when creating the element:
$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.
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. Otherwise the security system will intercept your create user requests.
http://symfony.com/doc/current/reference/forms/types/form.html#required