19

Here is written how to set the name of a form with a class:

http://symfony.com/doc/2.0/book/forms.html#creating-form-classes

but how to set the name of this form?

$form = $this->createFormBuilder($defaultData)
    ->add('name', 'text')
    ->add('email', 'email')
    ->getForm();

Well, I'm trying to get post parameters after submitting it this way:

$postData = $request->request->get('form_name');
j0k
  • 22,600
  • 28
  • 79
  • 90
tirenweb
  • 30,963
  • 73
  • 183
  • 303

5 Answers5

44

I would like to bring some more precision. At least, for the most recent version of Symfony (2.1), the correct symtax (documented on the API) is:

<?php

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

It is important because you can still pass options to the FormBuilder. For a more concrete example:

<?php

 $form = $this->get('form.factory')->createNamedBuilder('user', 'form',  null, array(
    'constraints' => $collectionConstraint,
))
->add('name', 'text')
->add('email', 'email')
->getForm();
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
tobalsan
  • 466
  • 4
  • 4
  • 6
    In `Symfony 3.1`, if you also want to set other options, the solution is: `$this->get('form.factory')->createNamedBuilder('yourFormName', FormType::class, null, ['option'=>'value']` – jxmallett Jun 16 '16 at 00:35
14

There is no shortcut method for this purpose. Instead you have to access the method createNamedBuilder in the form factory:

$this->get('form.factory')->createNamedBuilder('form', 'form_name', $defaultData)
    ->add('name', 'text')
    ->add('email', 'email')
    ->getForm();
Bernhard Schussek
  • 4,823
  • 26
  • 33
  • As far as I can see this is still valid for Symfony2.3, am I right? Still no shortcut for this. – ftassi Jun 15 '13 at 14:48
  • 6
    Just noted that in Symfony 2.3 the parameters are the other way around ... so its now `(name, type, defaults, options)` .. [see the docs](http://api.symfony.com/2.3/Symfony/Component/Form/FormFactory.html#method_createNamedBuilder) – Manse Sep 06 '13 at 14:46
4

If you're using Symfony 3.1, the field types have changed to use their explicit class (FormType, TextType, and EmailType) and the parameter position for the value of the form name attribute has switched places with the FormType parameter in the createNamedBuilder function.

$this->get('form.factory')
  ->createNamedBuilder('form_name', FormType::class, $defaultData)
  ->add('name', TextType::class)
  ->add('email', EmailType::class)
  ->getForm();
Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167
3

Is there any reason why you don't just do:

$data = $form->getData();
igorw
  • 27,759
  • 5
  • 78
  • 90
1

In version 2.4.1 of Symfony, the solution is:

$form = $this->createFormBuilder ( NULL, array ( 'attr' => array ( 'name' => 'myFormName', 'id' => 'myFormId' ) ) )
            ->add (..

You can also set other form attributes this way, but I've not tried. Replace NULL with your data if you want.

Rob Tyler
  • 31
  • 2