1
1
.. index ::
2
2
single: Doctrine; Simple Registration Form
3
3
single: Form; Simple Registration Form
4
+ single: Security; Simple Registration Form
4
5
5
- How to Implement a simple Registration Form
6
+ How to Implement a Simple Registration Form
6
7
===========================================
7
8
8
9
Creating a registration form is pretty easy - it *really * means just creating
9
- a form that will update some ``User `` model object (a Doctrine entity in this example)
10
- and then save it.
10
+ a form that will update some ``User `` model object (a Doctrine entity in this
11
+ example) and then save it.
11
12
12
13
.. tip ::
13
14
14
- The popular `FOSUserBundle `_ provides a registration form, reset password form
15
- and other user management functionality.
15
+ The popular `FOSUserBundle `_ provides a registration form, reset password
16
+ form and other user management functionality.
16
17
17
18
If you don't already have a ``User `` entity and a working login system,
18
19
first start with :doc: `/cookbook/security/entity_provider `.
@@ -61,27 +62,27 @@ With some validation added, your class may look something like this::
61
62
private $id;
62
63
63
64
/**
64
- * @ORM\Column(type="string", length=255)
65
+ * @ORM\Column(type="string", length=255, unique=true )
65
66
* @Assert\NotBlank()
66
67
* @Assert\Email()
67
68
*/
68
69
private $email;
69
70
70
71
/**
71
- * @ORM\Column(type="string", length=255)
72
+ * @ORM\Column(type="string", length=255, unique=true )
72
73
* @Assert\NotBlank()
73
74
*/
74
75
private $username;
75
76
76
77
/**
77
78
* @Assert\NotBlank()
78
- * @Assert\Length(max = 4096)
79
+ * @Assert\Length(max= 4096)
79
80
*/
80
81
private $plainPassword;
81
82
82
83
/**
83
84
* The below length depends on the "algorithm" you use for encoding
84
- * the password, but this works well with bcrypt
85
+ * the password, but this works well with bcrypt.
85
86
*
86
87
* @ORM\Column(type="string", length=64)
87
88
*/
@@ -124,6 +125,11 @@ With some validation added, your class may look something like this::
124
125
$this->password = $password;
125
126
}
126
127
128
+ public function getSalt()
129
+ {
130
+ return null;
131
+ }
132
+
127
133
// other methods, including security methods like getRoles()
128
134
}
129
135
@@ -146,8 +152,10 @@ example, see the :ref:`Entity Provider <security-crete-user-entity>` article.
146
152
only place where you don't need to worry about this is your login form,
147
153
since Symfony's Security component handles this for you.
148
154
149
- Create a Form for the Model
150
- ---------------------------
155
+ .. _create-a-form-for-the-model :
156
+
157
+ Create a Form for the Entity
158
+ ----------------------------
151
159
152
160
Next, create the form for the ``User `` entity::
153
161
@@ -196,8 +204,9 @@ There are just three fields: ``email``, ``username`` and ``plainPassword``
196
204
Handling the Form Submission
197
205
----------------------------
198
206
199
- Next, you need a controller to handle the form. Start by creating a simple
200
- controller for displaying the registration form::
207
+ Next, you need a controller to handle the form rendering and submission. If the
208
+ form is submitted, the controller performs the validation and saves the data
209
+ into the database::
201
210
202
211
// src/AppBundle/Controller/RegistrationController.php
203
212
namespace AppBundle\Controller;
@@ -223,6 +232,7 @@ controller for displaying the registration form::
223
232
// 2) handle the submit (will only happen on POST)
224
233
$form->handleRequest($request);
225
234
if ($form->isSubmitted() && $form->isValid()) {
235
+
226
236
// 3) Encode the password (you could also do this via Doctrine listener)
227
237
$encoder = $this->get('security.encoder_factory')
228
238
->getEncoder($user);
@@ -249,6 +259,45 @@ controller for displaying the registration form::
249
259
}
250
260
}
251
261
262
+ To define the algorithm used to encode the password in step 3 configure the
263
+ encoder in the security configuration:
264
+
265
+ .. configuration-block ::
266
+
267
+ .. code-block :: yaml
268
+
269
+ # app/config/security.yml
270
+ security :
271
+ encoders :
272
+ AppBundle\Entity\User : bcrypt
273
+
274
+ .. code-block :: xml
275
+
276
+ <!-- app/config/security.xml -->
277
+ <?xml version =" 1.0" charset =" UTF-8" ?>
278
+ <srv : container xmlns =" http://symfony.com/schema/dic/security"
279
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
280
+ xmlns : srv =" http://symfony.com/schema/dic/services"
281
+ xsi : schemaLocation =" http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd" >
282
+
283
+ <config >
284
+ <encoder class =" AppBundle\Entity\User" >bcrypt</encoder >
285
+ </config >
286
+ </srv : container >
287
+
288
+ .. code-block :: php
289
+
290
+ // app/config/security.php
291
+ $container->loadFromExtension('security', array(
292
+ 'encoders' => array(
293
+ 'AppBundle\Entity\User' => 'bcrypt',
294
+ ),
295
+ ));
296
+
297
+ In this case the recommended ``bcrypt `` algorithm is used. To learn more
298
+ about how to encode the users password have a look into the
299
+ :ref: `security chapter <book-security-encoding-user-password >`.
300
+
252
301
.. note ::
253
302
254
303
If you decide to NOT use annotation routing (shown above), then you'll
0 commit comments