3

So I want to make a page with a sign in (log in) form and a sign up (registration) form with Symfony2. And I'm wondering what would be the best way to achieve this?

I've considered setting the form attribute action="/pathToMyFormProcess" but I then face a problem: I want to show the same template (HTML page) in that action. But the "/pathToMyFormProcess" would be called in many different actions with different templates. Like a log in form that is included or rendered on every page. I could make a new path and action for every template. But as this example shows it would be very frustrating:

My signup action would be:

public function signupAction() {

    $loginForm = $this->get('loginform'); // Get the login form
    $signUpForm = $this->get('signupform'); // Get the signup form

    $loadData = ... /* Loading data needed for this page (could be last visitors, 
   countries, some information from database that should be displayed on the page) */

    return // SIGNUPTEMPLATE - array ( forms and data )

}

Then if someone used the loginform it would send them to for example /signup/login with the action loginAction in SignupController for example.

public function signupAction() {

    $loginForm = $this->get('loginform'); // Get the login form
    $handler = $this->get('loginhandler'); // Get the handler

    $process = $handler->process(); // Process the request, try to log in basicly.
    if($process) {
      return // redirect or something - no problem
    }

    // Loading again, writing exact same code as signupAction()
    $signUpForm = $this->get('signupform'); // Get the signup form

    $loadData = ...

    return // SIGNUPTEMPLATE - array ( forms and data )
}

So I am wondering if you have any better ideas for how to achieve this?

j0k
  • 22,600
  • 28
  • 79
  • 90
Dennefyren
  • 344
  • 1
  • 2
  • 8

4 Answers4

2

Another thing that you can do es render a form in your page, for example:

in your layout.html.twig

{{ render(controller('SiteBundle:Login:login')) }}

Then in your loginAction you render your login form.

return $this->render('SiteBundle:Default:login-box.html.twig', array(
        'last_username' => $lastUsername,
        'error'         => $error,
        'csrf_token' => $csrfToken,
    ));

And finally your login-box.html.twig have yor html form.

Pablo
  • 635
  • 1
  • 7
  • 19
0

Watch out for different in branches . I found that without any reason some strange changes come in newer version of SYMFONY. example branch 2.6

public FormBuilderInterface createNamedBuilder(string|int $name, string|FormTypeInterface $type = 'form', mixed $data = null, array $options = array())

but in branch 2.0

public FormBuilder createNamedBuilder(string|FormTypeInterface $type, string $name, mixed $data = null, array $options = array())

What cause my headache.

croonx
  • 139
  • 1
  • 6
0

Please see my response to that question in this topic, it describes how to create a single form Type and use it to generate the same form with different ID (but keep that ID based on ID of underlying entity) N amount of times:

Generate same form type on same page multiple times Symfony2

Community
  • 1
  • 1
Ethernal
  • 172
  • 1
  • 12
0

The accepted answer by Dennefyren is a dead link. I found the google cached page:

"With multiple forms the problem that is solved is submission of multiple forms persisting different sets of entities and properties depending to which form is submitted.

So here is the format that does the trick for multiple forms. I makes use of the createNamedBuilder method which gets the type forms for an entity of class A. It also receives as parameter the name of the form we would like to label it. So we define two forms in the code below, each including two different properties of different entity, although they could be the same. Then we make sure we receive the POSTed information writing a conditional from where we can handle each form separate and persist each corresponding entity discriminating according to which form was submitted. Lastly we see that we pass to the twig engine two variables for two separate forms."

public function multiformAction()
{
    $form1 = $this->get('form.factory')->createNamedBuilder($formTypeA, 'form1name')
        ->add('foo', 'text')
        ->getForm();

    $form2 = $this->get('form.factory')->createNamedBuilder($formTypeB, 'form2name')
        ->add('bar', 'text')
        ->getForm();

    if('POST' === $request->getMethod()) {

        if ($request->request->has('form1name') {
            // handle the first form
        }

        if ($request->request->has('form2name') {
            // handle the second form
        }
    }

    return array(
        'form1' => $form1->createView(),
        'form2' => $form2->createView()
    );  
}

"Notice also that the $request variable is the sf2 request object. This object has a property called request too. The type of this property is ParameterBag which in turn has a method called has which checks for the given key within the object.

Within the twig templates obviously we are going to call separately the code for each form like:"

{{ form_row(form1.foo) }}
...
{{ form_row(form2.bar) }}
J-C FOREST
  • 321
  • 4
  • 11