@@ -21,38 +21,40 @@ form in its own PHP class::
2121
2222 use Symfony\Component\Form\AbstractType;
2323 use Symfony\Component\Form\FormBuilderInterface;
24- use Symfony\Component\OptionsResolver\OptionsResolver;
25- use Symfony\Component\Form\Extension\Core\Type\TextareaType;
26- use Symfony\Component\Form\Extension\Core\Type\EmailType;
27- use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
24+ use Symfony\Component\OptionsResolver\OptionsResolverInterface;
2825
2926 class PostType extends AbstractType
3027 {
3128 public function buildForm(FormBuilderInterface $builder, array $options)
3229 {
3330 $builder
3431 ->add('title')
35- ->add('summary', TextareaType::class )
36- ->add('content', TextareaType::class )
37- ->add('authorEmail', EmailType::class )
38- ->add('publishedAt', DateTimeType::class )
32+ ->add('summary', 'textarea' )
33+ ->add('content', 'textarea' )
34+ ->add('authorEmail', 'email' )
35+ ->add('publishedAt', 'datetime' )
3936 ;
4037 }
4138
42- public function configureOptions(OptionsResolver $resolver)
39+ public function setDefaultOptions(OptionsResolverInterface $resolver)
4340 {
4441 $resolver->setDefaults(array(
4542 'data_class' => 'AppBundle\Entity\Post'
4643 ));
4744 }
45+
46+ public function getName()
47+ {
48+ return 'post';
49+ }
4850 }
4951
5052.. best-practice ::
5153
5254 Put the form type classes in the ``AppBundle\Form `` namespace, unless you
5355 use other custom form classes like data transformers.
5456
55- To use the class, use ``createForm() `` and pass the fully qualified class name ::
57+ To use the class, use ``createForm() `` and instantiate the new class::
5658
5759 // ...
5860 use AppBundle\Form\PostType;
@@ -61,7 +63,7 @@ To use the class, use ``createForm()`` and pass the fully qualified class name::
6163 public function newAction(Request $request)
6264 {
6365 $post = new Post();
64- $form = $this->createForm(PostType::class , $post);
66+ $form = $this->createForm(new PostType() , $post);
6567
6668 // ...
6769 }
@@ -71,9 +73,13 @@ Registering Forms as Services
7173
7274You can also
7375:ref: `register your form type as a service <form-cookbook-form-field-service >`.
74- This is only needed if your form type requires some dependencies to be injected
75- by the container, otherwise it is unnecessary overhead and therefore *not *
76- recommended to do this for all form type classes.
76+ But this is *not * recommended unless you plan to reuse the new form type in many
77+ places or embed it in other forms directly or via the
78+ :doc: `collection type </reference/forms/types/collection >`.
79+
80+ For most forms that are used only to edit or create something, registering
81+ the form as a service is over-kill, and makes it more difficult to figure
82+ out exactly which form class is being used in a controller.
7783
7884Form Button Configuration
7985-------------------------
@@ -85,10 +91,9 @@ makes them easier to re-use later.
8591
8692 Add buttons in the templates, not in the form classes or the controllers.
8793
88- The Symfony Form component allows you to add buttons as fields on your form.
89- This is a nice way to simplify the template that renders your form. But if you
90- add the buttons directly in your form class, this would effectively limit the
91- scope of that form:
94+ Since Symfony 2.3, you can add buttons as fields on your form. This is a nice
95+ way to simplify the template that renders your form. But if you add the buttons
96+ directly in your form class, this would effectively limit the scope of that form:
9297
9398.. code-block :: php
9499
@@ -98,7 +103,7 @@ scope of that form:
98103 {
99104 $builder
100105 // ...
101- ->add('save', SubmitType::class , array('label' => 'Create Post'))
106+ ->add('save', 'submit' , array('label' => 'Create Post'))
102107 ;
103108 }
104109
@@ -113,7 +118,6 @@ some developers configure form buttons in the controller::
113118
114119 use Symfony\Component\HttpFoundation\Request;
115120 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
116- use Symfony\Component\Form\Extension\Core\Type\SubmitType;
117121 use AppBundle\Entity\Post;
118122 use AppBundle\Form\PostType;
119123
@@ -124,8 +128,8 @@ some developers configure form buttons in the controller::
124128 public function newAction(Request $request)
125129 {
126130 $post = new Post();
127- $form = $this->createForm(PostType::class , $post);
128- $form->add('submit', SubmitType::class , array(
131+ $form = $this->createForm(new PostType() , $post);
132+ $form->add('submit', 'submit' , array(
129133 'label' => 'Create',
130134 'attr' => array('class' => 'btn btn-default pull-right')
131135 ));
@@ -209,3 +213,21 @@ Second, we recommend using ``$form->isSubmitted()`` in the ``if`` statement
209213for clarity. This isn't technically needed, since ``isValid() `` first calls
210214``isSubmitted() ``. But without this, the flow doesn't read well as it *looks *
211215like the form is *always * processed (even on the GET request).
216+
217+ Custom Form Field Types
218+ -----------------------
219+
220+ .. best-practice ::
221+
222+ Add the ``app_ `` prefix to your custom form field types to avoid collisions.
223+
224+ Custom form field types inherit from the ``AbstractType `` class, which defines the
225+ ``getName() `` method to configure the name of that form type. These names must
226+ be unique in the application.
227+
228+ If a custom form type uses the same name as any of the Symfony's built-in form
229+ types, it will override it. The same happens when the custom form type matches
230+ any of the types defined by the third-party bundles installed in your application.
231+
232+ Add the ``app_ `` prefix to your custom form field types to avoid name collisions
233+ that can lead to hard to debug errors.
0 commit comments