@@ -21,38 +21,40 @@ form in its own PHP class::
21
21
22
22
use Symfony\Component\Form\AbstractType;
23
23
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;
28
25
29
26
class PostType extends AbstractType
30
27
{
31
28
public function buildForm(FormBuilderInterface $builder, array $options)
32
29
{
33
30
$builder
34
31
->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' )
39
36
;
40
37
}
41
38
42
- public function configureOptions(OptionsResolver $resolver)
39
+ public function setDefaultOptions(OptionsResolverInterface $resolver)
43
40
{
44
41
$resolver->setDefaults(array(
45
42
'data_class' => 'AppBundle\Entity\Post'
46
43
));
47
44
}
45
+
46
+ public function getName()
47
+ {
48
+ return 'post';
49
+ }
48
50
}
49
51
50
52
.. best-practice ::
51
53
52
54
Put the form type classes in the ``AppBundle\Form `` namespace, unless you
53
55
use other custom form classes like data transformers.
54
56
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::
56
58
57
59
// ...
58
60
use AppBundle\Form\PostType;
@@ -61,7 +63,7 @@ To use the class, use ``createForm()`` and pass the fully qualified class name::
61
63
public function newAction(Request $request)
62
64
{
63
65
$post = new Post();
64
- $form = $this->createForm(PostType::class , $post);
66
+ $form = $this->createForm(new PostType() , $post);
65
67
66
68
// ...
67
69
}
@@ -71,9 +73,13 @@ Registering Forms as Services
71
73
72
74
You can also
73
75
: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.
77
83
78
84
Form Button Configuration
79
85
-------------------------
@@ -85,10 +91,9 @@ makes them easier to re-use later.
85
91
86
92
Add buttons in the templates, not in the form classes or the controllers.
87
93
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:
92
97
93
98
.. code-block :: php
94
99
@@ -98,7 +103,7 @@ scope of that form:
98
103
{
99
104
$builder
100
105
// ...
101
- ->add('save', SubmitType::class , array('label' => 'Create Post'))
106
+ ->add('save', 'submit' , array('label' => 'Create Post'))
102
107
;
103
108
}
104
109
@@ -113,7 +118,6 @@ some developers configure form buttons in the controller::
113
118
114
119
use Symfony\Component\HttpFoundation\Request;
115
120
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
116
- use Symfony\Component\Form\Extension\Core\Type\SubmitType;
117
121
use AppBundle\Entity\Post;
118
122
use AppBundle\Form\PostType;
119
123
@@ -124,8 +128,8 @@ some developers configure form buttons in the controller::
124
128
public function newAction(Request $request)
125
129
{
126
130
$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(
129
133
'label' => 'Create',
130
134
'attr' => array('class' => 'btn btn-default pull-right')
131
135
));
@@ -209,3 +213,21 @@ Second, we recommend using ``$form->isSubmitted()`` in the ``if`` statement
209
213
for clarity. This isn't technically needed, since ``isValid() `` first calls
210
214
``isSubmitted() ``. But without this, the flow doesn't read well as it *looks *
211
215
like 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