@@ -10,13 +10,13 @@ database. For example, you may want to create a registration form with some
10
10
extra fields (like a "terms accepted" checkbox field) and embed the form
11
11
that actually stores the account information.
12
12
13
- The simple User Model
14
- ---------------------
13
+ The simple User Entity
14
+ ----------------------
15
15
16
16
You have a simple ``User `` entity mapped to the database::
17
17
18
- // src/Acme/AccountBundle /Entity/User.php
19
- namespace Acme\AccountBundle \Entity;
18
+ // src/AppBundle /Entity/User.php
19
+ namespace AppBundle \Entity;
20
20
21
21
use Doctrine\ORM\Mapping as ORM;
22
22
use Symfony\Component\Validator\Constraints as Assert;
@@ -45,7 +45,7 @@ You have a simple ``User`` entity mapped to the database::
45
45
/**
46
46
* @ORM\Column(type="string", length=255)
47
47
* @Assert\NotBlank()
48
- * @Assert\Length(max = 4096)
48
+ * @Assert\Length(max= 4096)
49
49
*/
50
50
protected $plainPassword;
51
51
@@ -76,9 +76,9 @@ You have a simple ``User`` entity mapped to the database::
76
76
}
77
77
78
78
This ``User `` entity contains three fields and two of them (``email `` and
79
- ``plainPassword ``) should display on the form. The email property must be unique
80
- in the database, this is enforced by adding this validation at the top of
81
- the class.
79
+ ``plainPassword ``) should be displayed by the form. The email property must be
80
+ unique in the database, this is enforced by adding an `` @UniqueEntity ``
81
+ validation constraint at the top of the class.
82
82
83
83
.. note ::
84
84
@@ -101,13 +101,13 @@ the class.
101
101
only place where you don't need to worry about this is your login form,
102
102
since Symfony's Security component handles this for you.
103
103
104
- Create a Form for the Model
105
- ---------------------------
104
+ Create a Form for the Entity
105
+ ----------------------------
106
106
107
- Next, create the form for the ``User `` model ::
107
+ Next, create the form for the ``User `` entity ::
108
108
109
- // src/Acme/AccountBundle /Form/Type/UserType.php
110
- namespace Acme\AccountBundle \Form\Type;
109
+ // src/AppBundle /Form/Type/UserType.php
110
+ namespace AppBundle \Form\Type;
111
111
112
112
use Symfony\Component\Form\AbstractType;
113
113
use Symfony\Component\Form\FormBuilderInterface;
@@ -119,16 +119,16 @@ Next, create the form for the ``User`` model::
119
119
{
120
120
$builder->add('email', 'email');
121
121
$builder->add('plainPassword', 'repeated', array(
122
- 'first_name' => 'password',
123
- 'second_name' => 'confirm',
124
- 'type' => 'password',
122
+ 'first_name' => 'password',
123
+ 'second_name' => 'confirm',
124
+ 'type' => 'password',
125
125
));
126
126
}
127
127
128
128
public function setDefaultOptions(OptionsResolverInterface $resolver)
129
129
{
130
130
$resolver->setDefaults(array(
131
- 'data_class' => 'Acme\AccountBundle \Entity\User'
131
+ 'data_class' => 'AppBundle \Entity\User'
132
132
));
133
133
}
134
134
@@ -156,17 +156,17 @@ be stored in the database.
156
156
157
157
Start by creating a simple class which represents the "registration"::
158
158
159
- // src/Acme/AccountBundle /Form/Model/Registration.php
160
- namespace Acme\AccountBundle \Form\Model;
159
+ // src/AppBundle /Form/Model/Registration.php
160
+ namespace AppBundle \Form\Model;
161
161
162
162
use Symfony\Component\Validator\Constraints as Assert;
163
163
164
- use Acme\AccountBundle \Entity\User;
164
+ use AppBundle \Entity\User;
165
165
166
166
class Registration
167
167
{
168
168
/**
169
- * @Assert\Type(type="Acme\AccountBundle \Entity\User")
169
+ * @Assert\Type(type="AppBundle \Entity\User")
170
170
* @Assert\Valid()
171
171
*/
172
172
protected $user;
@@ -200,8 +200,8 @@ Start by creating a simple class which represents the "registration"::
200
200
201
201
Next, create the form for this ``Registration `` model::
202
202
203
- // src/Acme/AccountBundle /Form/Type/RegistrationType.php
204
- namespace Acme\AccountBundle \Form\Type;
203
+ // src/AppBundle /Form/Type/RegistrationType.php
204
+ namespace AppBundle \Form\Type;
205
205
206
206
use Symfony\Component\Form\AbstractType;
207
207
use Symfony\Component\Form\FormBuilderInterface;
@@ -211,11 +211,9 @@ Next, create the form for this ``Registration`` model::
211
211
public function buildForm(FormBuilderInterface $builder, array $options)
212
212
{
213
213
$builder->add('user', new UserType());
214
- $builder->add(
215
- 'terms',
216
- 'checkbox',
217
- array('property_path' => 'termsAccepted')
218
- );
214
+ $builder->add('terms', 'checkbox', array(
215
+ 'property_path' => 'termsAccepted',
216
+ ));
219
217
$builder->add('Register', 'submit');
220
218
}
221
219
@@ -233,28 +231,43 @@ of the ``User`` class.
233
231
Handling the Form Submission
234
232
----------------------------
235
233
236
- Next, you need a controller to handle the form. Start by creating a simple
237
- controller for displaying the registration form::
234
+ Next, you need a controller to handle the form rendering and submission. If the
235
+ form is submitted, the controller performs the validation and saves the data
236
+ into the database::
238
237
239
- // src/Acme/AccountBundle /Controller/AccountController.php
240
- namespace Acme\AccountBundle \Controller;
238
+ // src/AppBundle /Controller/AccountController.php
239
+ namespace AppBundle \Controller;
241
240
242
241
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
242
+ use Symfony\Component\HttpFoundation\Request;
243
+ use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
243
244
244
- use Acme\AccountBundle \Form\Type\RegistrationType;
245
- use Acme\AccountBundle \Form\Model\Registration;
245
+ use AppBundle \Form\Type\RegistrationType;
246
+ use AppBundle \Form\Model\Registration;
246
247
247
248
class AccountController extends Controller
248
249
{
249
- public function registerAction()
250
+ /**
251
+ * @Route("/register", name="account_register")
252
+ */
253
+ public function registerAction(Request $request)
250
254
{
251
- $registration = new Registration();
252
- $form = $this->createForm(new RegistrationType(), $registration, array(
253
- 'action' => $this->generateUrl('account_create'),
254
- ));
255
+ $form = $this->createForm(new RegistrationType(), new Registration());
256
+
257
+ $form->handleRequest($request);
258
+
259
+ if ($form->isSubmitted() && $form->isValid()) {
260
+ $registration = $form->getData();
261
+
262
+ $em = $this->getDoctrine()->getManager();
263
+ $em->persist($registration->getUser());
264
+ $em->flush();
265
+
266
+ return $this->redirect($this->generateUrl('homepage'));
267
+ }
255
268
256
269
return $this->render(
257
- 'AcmeAccountBundle:Account: register.html.twig',
270
+ 'account/ register.html.twig',
258
271
array('form' => $form->createView())
259
272
);
260
273
}
@@ -264,90 +277,15 @@ And its template:
264
277
265
278
.. code-block :: html+jinja
266
279
267
- {# src/Acme/AccountBundle/ Resources/views/Account /register.html.twig #}
280
+ {# app/ Resources/views/account /register.html.twig #}
268
281
{{ form(form) }}
269
282
270
- Next, create the controller which handles the form submission. This performs
271
- the validation and saves the data into the database::
272
-
273
- use Symfony\Component\HttpFoundation\Request;
274
- // ...
275
-
276
- public function createAction(Request $request)
277
- {
278
- $em = $this->getDoctrine()->getManager();
279
-
280
- $form = $this->createForm(new RegistrationType(), new Registration());
281
-
282
- $form->handleRequest($request);
283
-
284
- if ($form->isValid()) {
285
- $registration = $form->getData();
286
-
287
- $em->persist($registration->getUser());
288
- $em->flush();
289
-
290
- return $this->redirect(...);
291
- }
292
-
293
- return $this->render(
294
- 'AcmeAccountBundle:Account:register.html.twig',
295
- array('form' => $form->createView())
296
- );
297
- }
298
-
299
283
Add new Routes
300
284
--------------
301
285
302
- Next, update your routes. If you're placing your routes inside your bundle
303
- (as shown here), don't forget to make sure that the routing file is being
304
- :ref: `imported <routing-include-external-resources >`.
305
-
306
- .. configuration-block ::
307
-
308
- .. code-block :: yaml
309
-
310
- # src/Acme/AccountBundle/Resources/config/routing.yml
311
- account_register :
312
- path : /register
313
- defaults : { _controller: AcmeAccountBundle:Account:register }
314
-
315
- account_create :
316
- path : /register/create
317
- defaults : { _controller: AcmeAccountBundle:Account:create }
318
-
319
- .. code-block :: xml
320
-
321
- <!-- src/Acme/AccountBundle/Resources/config/routing.xml -->
322
- <?xml version =" 1.0" encoding =" UTF-8" ?>
323
- <routes xmlns =" http://symfony.com/schema/routing"
324
- xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
325
- xsi : schemaLocation =" http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd" >
326
-
327
- <route id =" account_register" path =" /register" >
328
- <default key =" _controller" >AcmeAccountBundle:Account:register</default >
329
- </route >
330
-
331
- <route id =" account_create" path =" /register/create" >
332
- <default key =" _controller" >AcmeAccountBundle:Account:create</default >
333
- </route >
334
- </routes >
335
-
336
- .. code-block :: php
337
-
338
- // src/Acme/AccountBundle/Resources/config/routing.php
339
- use Symfony\Component\Routing\RouteCollection;
340
- use Symfony\Component\Routing\Route;
341
-
342
- $collection = new RouteCollection();
343
- $collection->add('account_register', new Route('/register', array(
344
- '_controller' => 'AcmeAccountBundle:Account:register',
345
- )));
346
- $collection->add('account_create', new Route('/register/create', array(
347
- '_controller' => 'AcmeAccountBundle:Account:create',
348
- )));
349
-
350
- return $collection;
286
+ Don't forget to make sure that the routes defined as annotations in your
287
+ controller are :ref: `loaded <routing-include-external-resources >` by your main
288
+ routing configuration file.
351
289
352
290
Update your Database Schema
353
291
---------------------------
0 commit comments