@@ -4,12 +4,12 @@ How to implement a simple Registration Form with MongoDB
4
4
Some forms have extra fields whose values don't need to be stored in the
5
5
database. In this example, we'll create a registration form with some extra
6
6
fields and (like a "terms accepted" checkbox field) and embed the form that
7
- actually stores the account information. We'll use MongoDB for storing the data.
7
+ actually stores the account information. We'll use MongoDB for storing the data.
8
8
9
9
.. tip ::
10
10
11
11
If you are not familiar with Doctrine's MongoDB library, read
12
- ":doc: `/cookbook/doctrine/mongodb `" cookbook entry first to learn
12
+ ":doc: `/cookbook/doctrine/mongodb `" cookbook entry first to learn
13
13
how to setup and work with MongoDB inside Symfony.
14
14
15
15
The simple User model
@@ -40,44 +40,44 @@ So, in this tutorial we begin with the model for a ``User`` document::
40
40
* @Assert\NotBlank()
41
41
* @Assert\Email()
42
42
*/
43
- protected $email;
44
-
43
+ protected $email;
44
+
45
45
/**
46
46
* @MongoDB\Field(type="string")
47
47
* @Assert\NotBlank()
48
48
*/
49
- protected $password;
50
-
49
+ protected $password;
50
+
51
51
public function getId()
52
52
{
53
- return $this->id;
53
+ return $this->id;
54
54
}
55
-
55
+
56
56
public function getEmail()
57
57
{
58
- return $this->email;
58
+ return $this->email;
59
59
}
60
-
60
+
61
61
public function setEmail($email)
62
62
{
63
- $this->email = $email;
63
+ $this->email = $email;
64
64
}
65
-
65
+
66
66
public function getPassword()
67
67
{
68
- return $this->password;
68
+ return $this->password;
69
69
}
70
-
70
+
71
71
// stupid simple encryption (please don't copy it!)
72
72
public function setPassword($password)
73
73
{
74
- $this->password = sha1($password);
74
+ $this->password = sha1($password);
75
75
}
76
76
}
77
77
78
78
This ``User `` document contains three fields and two of them (email and
79
- password) should display on the form. The email property must be unique
80
- on the database, so we've added this validation at the top of the class.
79
+ password) should display on the form. The email property must be unique
80
+ on the database, so we've added this validation at the top of the class.
81
81
82
82
.. note ::
83
83
@@ -90,38 +90,38 @@ Create a Form for the Model
90
90
91
91
Next, create the form for the ``User `` model::
92
92
93
- // src/Acme/AccountBundle/Form/UserType.php
94
- namespace Acme\AccountBundle\Form;
93
+ // src/Acme/AccountBundle/Form/Type/ UserType.php
94
+ namespace Acme\AccountBundle\Form\Type;
95
95
96
96
use Symfony\Component\Form\AbstractType;
97
- use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
98
- use Symfony\Component\Form\FormBuilder;
97
+ use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
98
+ use Symfony\Component\Form\FormBuilder;
99
99
100
100
class UserType extends AbstractType
101
101
{
102
102
public function buildForm(FormBuilder $builder, array $options)
103
103
{
104
- $builder->add('email', 'email');
104
+ $builder->add('email', 'email');
105
105
$builder->add('password', 'repeated', array(
106
- 'first_name' => 'password',
107
- 'second_name' => 'confirm',
106
+ 'first_name' => 'password',
107
+ 'second_name' => 'confirm',
108
108
'type' => 'password'
109
- ));
109
+ ));
110
110
}
111
-
111
+
112
112
public function getDefaultOptions(array $options)
113
113
{
114
114
return array('data_class' => 'Acme\AccountBundle\Document\User');
115
115
}
116
116
}
117
117
118
- We just added two fields: email and password (repeated to confirm the entered
118
+ We just added two fields: email and password (repeated to confirm the entered
119
119
password). The ``data_class `` option tells the form the name of data class
120
120
(i.e. your ``User `` document).
121
121
122
122
.. tip ::
123
123
124
- To explore more things about form component, read this documentation :doc: `file</book/forms> `.
124
+ To explore more things about form component, read this documentation :doc: `file</book/forms> `.
125
125
126
126
Embedding the User form into a Registration Form
127
127
------------------------------------------------
@@ -135,55 +135,55 @@ In other words, create a second form for registration, which embeds the ``User``
135
135
form and adds the extra field needed. Start by creating a simple class which
136
136
represents the "registration"::
137
137
138
- // src/Acme/AccountBundle/Form/Registration.php
139
- namespace Acme\AccountBundle\Form;
138
+ // src/Acme/AccountBundle/Form/Model/ Registration.php
139
+ namespace Acme\AccountBundle\Form\Model ;
140
140
141
141
use Symfony\Component\Validator\Constraints as Assert;
142
142
143
143
use Acme\AccountBundle\Document\User;
144
144
145
145
class Registration
146
- {
146
+ {
147
147
/**
148
148
* @Assert\Type(type="Acme\AccountBundle\Document\User")
149
149
*/
150
- protected $user;
151
-
150
+ protected $user;
151
+
152
152
/**
153
153
* @Assert\NotBlank()
154
154
* @Assert\True()
155
155
*/
156
156
protected $termsAccepted;
157
-
157
+
158
158
public function setUser(User $user)
159
159
{
160
- $this->user = $user;
160
+ $this->user = $user;
161
161
}
162
-
162
+
163
163
public function getUser()
164
164
{
165
- return $this->user;
165
+ return $this->user;
166
166
}
167
-
167
+
168
168
public function getTermsAccepted()
169
169
{
170
170
return $this->termsAccepted;
171
171
}
172
-
172
+
173
173
public function setTermsAccepted($termsAccepted)
174
174
{
175
- $this->termsAccepted = (boolean)$termsAccepted;
175
+ $this->termsAccepted = (boolean)$termsAccepted;
176
176
}
177
177
}
178
178
179
179
Next, create the form for this ``Registration `` model::
180
180
181
- // src/Acme/AccountBundle/Form/RegistrationType.php
182
- namespace Acme\AccountBundle\Form;
181
+ // src/Acme/AccountBundle/Form/Type/ RegistrationType.php
182
+ namespace Acme\AccountBundle\Form\Type;
183
183
184
184
use Symfony\Component\Form\AbstractType;
185
- use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
186
- use Symfony\Component\Form\FormBuilder;
185
+ use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
186
+ use Symfony\Component\Form\FormBuilder;
187
187
188
188
class RegistrationType extends AbstractType
189
189
{
@@ -209,50 +209,53 @@ controller for displaying the registration form::
209
209
namespace Acme\AccountBundle\Controller;
210
210
211
211
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
212
- use Symfony\Component\HttpFoundation\Response;
212
+ use Symfony\Component\HttpFoundation\Response;
213
213
214
- use Acme\AccountBundle\Form;
214
+ use Acme\AccountBundle\Form\Type\RegistrationType;
215
+ use Acme\AccountBundle\Form\Model\Registration;
215
216
216
217
class AccountController extends Controller
217
218
{
218
219
public function registerAction()
219
220
{
220
- $form = $this->createForm(new Form\ RegistrationType(), new Form\ Registration());
221
-
221
+ $form = $this->createForm(new RegistrationType(), new Registration());
222
+
222
223
return $this->render('AcmeAccountBundle:Account:register.html.twig', array('form' => $form->createView()));
223
224
}
224
225
}
225
226
226
- and it's template::
227
+ and it's template:
228
+
229
+ .. code-block :: html+jinja
227
230
228
231
{# src/Acme/AccountBundle/Resources/views/Account/register.html.twig #}
229
232
230
233
<form action="{{ path('create')}}" method="post" {{ form_enctype(form) }}>
231
234
{{ form_widget(form) }}
232
235
233
236
<input type="submit" />
234
- </form>
237
+ </form>
235
238
236
239
Finally, create the controller which handles the form submission. This performs
237
240
the validation and saves the data into MongoDB::
238
241
239
242
public function createAction()
240
243
{
241
244
$dm = $this->get('doctrine.odm.mongodb.default_document_manager');
242
-
243
- $form = $this->createForm(new Form\ RegistrationType(), new Form\ Registration());
244
-
245
- $form->bindRequest($this->get('request'));
246
-
245
+
246
+ $form = $this->createForm(new RegistrationType(), new Registration());
247
+
248
+ $form->bindRequest($this->get('request'));
249
+
247
250
if ($form->isValid()) {
248
251
$registration = $form->getData();
249
-
250
- $dm->persist($registration->getUser());
252
+
253
+ $dm->persist($registration->getUser());
251
254
$dm->flush();
252
-
255
+
253
256
return $this->redirect(...);
254
257
}
255
-
258
+
256
259
return $this->render('AcmeAccountBundle:Account:register.html.twig', array('form' => $form->createView()));
257
260
}
258
261
0 commit comments