@@ -18,6 +18,8 @@ the object.
18
18
Let's see how this works in a practical example. Let's create a simple
19
19
``Customer `` class::
20
20
21
+ namespace Application\HelloBundle\Entity;
22
+
21
23
class Customer
22
24
{
23
25
public $name;
@@ -40,6 +42,12 @@ is public, while ``$age`` can only be modified through setters and getters.
40
42
Now let's create a form to let the visitor fill the data of the object::
41
43
42
44
// src/Application/HelloBundle/Controller/HelloController.php
45
+
46
+ use Application\HelloBundle\Entity\Customer;
47
+ use Symfony\Component\Form\Form;
48
+ use Symfony\Component\Form\TextField;
49
+ use Symfony\Component\Form\IntegerField;
50
+
43
51
public function signupAction()
44
52
{
45
53
$customer = new Customer();
@@ -49,7 +57,7 @@ Now let's create a form to let the visitor fill the data of the object::
49
57
$form->add(new IntegerField('age'));
50
58
51
59
return $this->render('HelloBundle:Hello:signup.php', array(
52
- 'form' => $this->get('templating. form')->get($form)
60
+ 'form' => $form
53
61
));
54
62
}
55
63
@@ -68,17 +76,15 @@ Let's create a simple template to render the form:
68
76
# src/Application/HelloBundle/Resources/views/Hello/signup.php
69
77
<?php $view->extend('HelloBundle::layout.php') ?>
70
78
71
- <?php echo $ form->form('#') ? >
72
- <?php echo $form->render() ?>
79
+ <form action="#" method="post" >
80
+ <?php echo $view[' form'] ->render($form ) ?>
73
81
74
82
<input type="submit" value="Send!" />
75
83
</form>
76
84
77
85
.. note ::
78
86
79
- Form rendering in templates is covered in two dedicated chapters: one for
80
- :doc: `PHP templates </guides/forms/view >`, and one for :doc: `Twig templates
81
- </guides/forms/twig>`.
87
+ Form rendering in templates is covered in chapter :doc: `PHP templates </guides/forms/view >`.
82
88
83
89
When the user submits the form, we also need to handle the submitted data. All
84
90
the data is stored in a POST parameter with the name of the form::
@@ -182,6 +188,9 @@ We can use a field group to show fields for the customer and the nested
182
188
address at the same time::
183
189
184
190
# src/Application/HelloBundle/Controller/HelloController.php
191
+
192
+ use Symfony\Component\Form\FieldGroup;
193
+
185
194
public function signupAction()
186
195
{
187
196
$customer = new Customer();
@@ -207,6 +216,8 @@ The ``RepeatedField`` is an extended field group that allows you to output a
207
216
field twice. The repeated field will only validate if the user enters the same
208
217
value in both fields::
209
218
219
+ use Symfony\Component\Form\RepeatedField;
220
+
210
221
$form->add(new RepeatedField(new TextField('email')));
211
222
212
223
This is a very useful field for querying email addresses or passwords!
@@ -227,6 +238,8 @@ will extend the ``Customer`` class to store three email addresses::
227
238
228
239
We will now add a ``CollectionField `` to manipulate these addresses::
229
240
241
+ use Symfony\Component\Form\CollectionField;
242
+
230
243
$form->add(new CollectionField(new TextField('emails')));
231
244
232
245
If you set the option "modifiable" to ``true ``, you can even add or remove
@@ -254,6 +267,8 @@ accepting terms and conditions.
254
267
255
268
Let's create a simple ``Registration `` class for this purpose::
256
269
270
+ namespace Application\HelloBundle\Entity;
271
+
257
272
class Registration
258
273
{
259
274
/** @validation:Valid */
@@ -271,6 +286,10 @@ Let's create a simple ``Registration`` class for this purpose::
271
286
Now we can easily adapt the form in the controller::
272
287
273
288
# src/Application/HelloBundle/Controller/HelloController.php
289
+
290
+ use Application\HelloBundle\Entity\Registration;
291
+ use Symfony\Component\Form\CheckboxField;
292
+
274
293
public function signupAction()
275
294
{
276
295
$registration = new Registration();
0 commit comments