@@ -11,7 +11,7 @@ Your First Form
11
11
---------------
12
12
13
13
A form in Symfony2 is a transparent layer on top of your domain model. It
14
- reads properties from an object, displays the values in the form and allows
14
+ reads properties from an object, displays the values in the form, and allows
15
15
the user to change them. When the form is submitted, the values are written
16
16
back into the object.
17
17
@@ -21,7 +21,6 @@ Let's see how this works in a practical example. Let's create a simple
21
21
class Customer
22
22
{
23
23
public $name;
24
-
25
24
private $age = 20;
26
25
27
26
public function getAge()
@@ -34,9 +33,9 @@ Let's see how this works in a practical example. Let's create a simple
34
33
$this->age = $age;
35
34
}
36
35
}
37
-
38
- The class contains two properties ``name `` and "age". The property ``$name `` is
39
- public, while ``$age `` can only be modified through setters and getters.
36
+
37
+ The class contains two properties ``name `` and "age". The property ``$name ``
38
+ is public, while ``$age `` can only be modified through setters and getters.
40
39
41
40
Now let's create a form to let the visitor fill the data of the object::
42
41
@@ -49,12 +48,12 @@ Now let's create a form to let the visitor fill the data of the object::
49
48
$form->add(new TextField('name'));
50
49
$form->add(new IntegerField('age'));
51
50
52
- return $this->render('HelloBundle:Hello:signup', array('form' => $form));
51
+ return $this->render('HelloBundle:Hello:signup.php ', array('form' => $form));
53
52
}
54
-
53
+
55
54
A form consists of various fields. Each field represents a property in your
56
55
class. The property must have the same name as the field and must either be
57
- public or accessible through public getters and setters. Now let's create a
56
+ public or accessible through public getters and setters. Now let's create a
58
57
simple template to render the form:
59
58
60
59
.. code-block :: html+php
@@ -67,9 +66,9 @@ simple template to render the form:
67
66
<?php echo $form->render() ?>
68
67
<input type="submit" value="Send!" />
69
68
</form>
70
-
71
- When the user submits the form, we also need to handle the submitted data.
72
- All the data is stored in a POST parameter with the name of the form::
69
+
70
+ When the user submits the form, we also need to handle the submitted data. All
71
+ the data is stored in a POST parameter with the name of the form::
73
72
74
73
# src/Application/HelloBundle/Controller/HelloController.php
75
74
public function signupAction()
@@ -79,17 +78,17 @@ All the data is stored in a POST parameter with the name of the form::
79
78
80
79
// form setup...
81
80
82
- if ($this['request']->getMethod() == 'POST' ) {
83
- $form->bind($this['request']->getParameter ('customer'));
81
+ if ('POST' === $this['request']->getMethod()) {
82
+ $form->bind($this['request']->request->get ('customer'));
84
83
85
84
if ($form->isValid()) {
86
85
// save $customer object and redirect
87
86
}
88
87
}
89
88
90
- return $this->render('HelloBundle:Hello:signup', array('form' => $form));
89
+ return $this->render('HelloBundle:Hello:signup.php ', array('form' => $form));
91
90
}
92
-
91
+
93
92
Congratulations! You just created your first fully-functional form with
94
93
Symfony2.
95
94
@@ -99,15 +98,17 @@ Symfony2.
99
98
Form Fields
100
99
-----------
101
100
102
- As you have learned, a form consists of one or more form fields. In Symfony2,
101
+ As you have learned, a form consists of one or more form fields. In Symfony2,
103
102
form fields have two responsibilities:
104
103
105
- * Render HTML
106
- * Convert data between normalized and humane representations
104
+ * Render HTML;
105
+
106
+ * Convert data between normalized and humane representations.
107
107
108
- Let's look at the ``DateField `` for example. While you probably prefer to store
109
- dates as strings or ``DateTime `` objects, users rather like to choose them from a
110
- list of drop downs. ``DateField `` handles the rendering and type conversion for you.
108
+ Let's look at the ``DateField `` for example. While you probably prefer to
109
+ store dates as strings or ``DateTime `` objects, users rather like to choose
110
+ them from a list of drop downs. ``DateField `` handles the rendering and type
111
+ conversion for you.
111
112
112
113
Basic Fields
113
114
~~~~~~~~~~~~
@@ -186,16 +187,16 @@ at the same time::
186
187
187
188
// process form ...
188
189
}
189
-
190
+
190
191
With only these little changes you can now edit also the ``Address `` object!
191
192
Cool, ey?
192
193
193
194
Repeated Fields
194
195
~~~~~~~~~~~~~~~
195
196
196
- The ``RepeatedField `` is an extended field group that allows you to output a field
197
- twice. The repeated field will only validate if the user enters the same value
198
- in both fields::
197
+ The ``RepeatedField `` is an extended field group that allows you to output a
198
+ field twice. The repeated field will only validate if the user enters the same
199
+ value in both fields::
199
200
200
201
$form->add(new RepeatedField(new TextField('email')));
201
202
@@ -205,7 +206,7 @@ Collection Fields
205
206
~~~~~~~~~~~~~~~~~
206
207
207
208
The ``CollectionField `` is a special field group for manipulating arrays or
208
- objects that implement the interface ``Traversable ``. To demonstrate this, we
209
+ objects that implement the interface ``Traversable ``. To demonstrate this, we
209
210
will extend the ``Customer `` class to store three email addresses::
210
211
211
212
class Customer
@@ -219,9 +220,9 @@ We will now add a ``CollectionField`` to manipulate these addresses::
219
220
220
221
$form->add(new CollectionField(new TextField('emails')));
221
222
222
- If you set the option "modifiable" to ``true ``, you can even add or remove rows
223
- in the collection via Javascript! The ``CollectionField `` will notice it and
224
- resize the underlying array accordingly.
223
+ If you set the option "modifiable" to ``true ``, you can even add or remove
224
+ rows in the collection via Javascript! The ``CollectionField `` will notice it
225
+ and resize the underlying array accordingly.
225
226
226
227
.. index ::
227
228
single: Forms; Validation
@@ -230,7 +231,7 @@ Form Validation
230
231
---------------
231
232
232
233
You have already learned in the last part of this tutorial how to set up
233
- validation constraints for a PHP class. The nice thing is that this is enough
234
+ validation constraints for a PHP class. The nice thing is that this is enough
234
235
to validate a Form! Remember that a form is nothing more than a gateway for
235
236
changing data in an object.
236
237
@@ -275,19 +276,19 @@ Now we can easily adapt the form in the controller::
275
276
276
277
$form->add($group);
277
278
278
- if ($this['request']->getMethod() == 'POST' ) {
279
- $form->bind($this['request']->getParameter ('customer'));
279
+ if ('POST' === $this['request']->getMethod()) {
280
+ $form->bind($this['request']->request->get ('customer'));
280
281
281
282
if ($form->isValid()) {
282
283
$registration->process();
283
284
}
284
285
}
285
286
286
- return $this->render('HelloBundle:Hello:signup', array('form' => $form));
287
+ return $this->render('HelloBundle:Hello:signup.php ', array('form' => $form));
287
288
}
288
-
289
+
289
290
The big benefit of this refactoring is that we can reuse the ``Registration ``
290
- class. Extending the application to allow users to sign up via XML is no
291
+ class. Extending the application to allow users to sign up via XML is no
291
292
problem at all!
292
293
293
294
.. index ::
@@ -296,11 +297,11 @@ problem at all!
296
297
Customizing the View
297
298
--------------------
298
299
299
- Unfortunately the output of ``$form->render() `` doesn't look too great. Symfony
300
- 2.0 makes it very easy though to customize the HTML of a form. You can access
301
- every field and field group in the form by its name. All fields offer the
302
- method ``render() `` for rendering the widget and ``renderErrors() `` for rendering
303
- a ``<ul> ``-list with the field errors.
300
+ Unfortunately the output of ``$form->render() `` doesn't look too great.
301
+ Symfony 2.0 makes it very easy though to customize the HTML of a form. You can
302
+ access every field and field group in the form by its name. All fields offer
303
+ the method ``render() `` for rendering the widget and ``renderErrors() `` for
304
+ rendering a ``<ul> ``-list with the field errors.
304
305
305
306
The following example shows you how to refine the HTML of an individual form
306
307
field::
@@ -333,8 +334,8 @@ for your hidden fields:
333
334
<div class="form-row">
334
335
...
335
336
</div>
336
- <?php endif ?>
337
- <?php endforeach ?>
337
+ <?php endif; ?>
338
+ <?php endforeach; ?>
338
339
339
340
By using plain HTML, you have the greatest possible flexibility in designing
340
341
your forms. Especially your designers will be happy that they can manipulate
@@ -346,6 +347,6 @@ Final Thoughts
346
347
This chapter showed you how the Form component of Symfony2 can help you to
347
348
rapidly create forms for your domain objects. The component embraces a strict
348
349
separation between business logic and presentation. Many fields are
349
- automatically localized to make your visitors feel comfortable on your website.
350
- And with the new architecture, this is just the beginning of many new, mighty
351
- user-created fields!
350
+ automatically localized to make your visitors feel comfortable on your
351
+ website. And with the new architecture, this is just the beginning of many
352
+ new, mighty user-created fields!
0 commit comments