Skip to content

Commit 1ac8710

Browse files
committed
[Cookbook] Make registration_form follow best practices
1 parent 99aca45 commit 1ac8710

File tree

1 file changed

+57
-119
lines changed

1 file changed

+57
-119
lines changed

cookbook/doctrine/registration_form.rst

Lines changed: 57 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ database. For example, you may want to create a registration form with some
1010
extra fields (like a "terms accepted" checkbox field) and embed the form
1111
that actually stores the account information.
1212

13-
The simple User Model
14-
---------------------
13+
The simple User Entity
14+
----------------------
1515

1616
You have a simple ``User`` entity mapped to the database::
1717

18-
// src/Acme/AccountBundle/Entity/User.php
19-
namespace Acme\AccountBundle\Entity;
18+
// src/AppBundle/Entity/User.php
19+
namespace AppBundle\Entity;
2020

2121
use Doctrine\ORM\Mapping as ORM;
2222
use Symfony\Component\Validator\Constraints as Assert;
@@ -45,7 +45,7 @@ You have a simple ``User`` entity mapped to the database::
4545
/**
4646
* @ORM\Column(type="string", length=255)
4747
* @Assert\NotBlank()
48-
* @Assert\Length(max = 4096)
48+
* @Assert\Length(max=4096)
4949
*/
5050
protected $plainPassword;
5151

@@ -76,9 +76,9 @@ You have a simple ``User`` entity mapped to the database::
7676
}
7777

7878
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.
8282

8383
.. note::
8484

@@ -101,13 +101,13 @@ the class.
101101
only place where you don't need to worry about this is your login form,
102102
since Symfony's Security component handles this for you.
103103

104-
Create a Form for the Model
105-
---------------------------
104+
Create a Form for the Entity
105+
----------------------------
106106

107-
Next, create the form for the ``User`` model::
107+
Next, create the form for the ``User`` entity::
108108

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;
111111

112112
use Symfony\Component\Form\AbstractType;
113113
use Symfony\Component\Form\FormBuilderInterface;
@@ -119,16 +119,16 @@ Next, create the form for the ``User`` model::
119119
{
120120
$builder->add('email', 'email');
121121
$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',
125125
));
126126
}
127127

128128
public function setDefaultOptions(OptionsResolverInterface $resolver)
129129
{
130130
$resolver->setDefaults(array(
131-
'data_class' => 'Acme\AccountBundle\Entity\User'
131+
'data_class' => 'AppBundle\Entity\User'
132132
));
133133
}
134134

@@ -156,17 +156,17 @@ be stored in the database.
156156

157157
Start by creating a simple class which represents the "registration"::
158158

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;
161161

162162
use Symfony\Component\Validator\Constraints as Assert;
163163

164-
use Acme\AccountBundle\Entity\User;
164+
use AppBundle\Entity\User;
165165

166166
class Registration
167167
{
168168
/**
169-
* @Assert\Type(type="Acme\AccountBundle\Entity\User")
169+
* @Assert\Type(type="AppBundle\Entity\User")
170170
* @Assert\Valid()
171171
*/
172172
protected $user;
@@ -200,8 +200,8 @@ Start by creating a simple class which represents the "registration"::
200200

201201
Next, create the form for this ``Registration`` model::
202202

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;
205205

206206
use Symfony\Component\Form\AbstractType;
207207
use Symfony\Component\Form\FormBuilderInterface;
@@ -211,11 +211,9 @@ Next, create the form for this ``Registration`` model::
211211
public function buildForm(FormBuilderInterface $builder, array $options)
212212
{
213213
$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+
));
219217
$builder->add('Register', 'submit');
220218
}
221219

@@ -233,28 +231,43 @@ of the ``User`` class.
233231
Handling the Form Submission
234232
----------------------------
235233

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::
238237

239-
// src/Acme/AccountBundle/Controller/AccountController.php
240-
namespace Acme\AccountBundle\Controller;
238+
// src/AppBundle/Controller/AccountController.php
239+
namespace AppBundle\Controller;
241240

242241
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
242+
use Symfony\Component\HttpFoundation\Request;
243+
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
243244

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;
246247

247248
class AccountController extends Controller
248249
{
249-
public function registerAction()
250+
/**
251+
* @Route("/register", name="account_register")
252+
*/
253+
public function registerAction(Request $request)
250254
{
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+
}
255268

256269
return $this->render(
257-
'AcmeAccountBundle:Account:register.html.twig',
270+
'account/register.html.twig',
258271
array('form' => $form->createView())
259272
);
260273
}
@@ -264,90 +277,15 @@ And its template:
264277

265278
.. code-block:: html+jinja
266279

267-
{# src/Acme/AccountBundle/Resources/views/Account/register.html.twig #}
280+
{# app/Resources/views/account/register.html.twig #}
268281
{{ form(form) }}
269282

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-
299283
Add new Routes
300284
--------------
301285

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.
351289

352290
Update your Database Schema
353291
---------------------------

0 commit comments

Comments
 (0)