Skip to content

Commit 25f43e7

Browse files
committed
Changed the namespaces of form related classes in the bundle
The types are now placed in Form\Type and the form-specific non-persisted models in Form\Model to keep the namespace organized
1 parent 8e66cc9 commit 25f43e7

File tree

2 files changed

+67
-64
lines changed

2 files changed

+67
-64
lines changed

book/forms.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -629,9 +629,9 @@ that will house the logic for building the product form:
629629

630630
.. code-block:: php
631631
632-
// src/Acme/StoreBundle/Form/ProductType.php
632+
// src/Acme/StoreBundle/Form/Type/ProductType.php
633633
634-
namespace Acme\StoreBundle\Form;
634+
namespace Acme\StoreBundle\Form\Type;
635635
636636
use Symfony\Component\Form\AbstractType;
637637
use Symfony\Component\Form\FormBuilder;
@@ -653,7 +653,7 @@ It can be used to quickly build a form object in the controller:
653653
// src/Acme/StoreBundle/Controller/DefaultController.php
654654
655655
// add this new use statement at the top of the class
656-
use Acme\StoreBundle\Form\ProductType;
656+
use Acme\StoreBundle\Form\Type\ProductType;
657657
658658
public function indexAction()
659659
{
@@ -796,8 +796,8 @@ create a form class so that a ``Category`` object can be modified by the user:
796796

797797
.. code-block:: php
798798
799-
// src/Acme/StoreBundle/Form/CategoryType.php
800-
namespace Acme\StoreBundle\Form;
799+
// src/Acme/StoreBundle/Form/Type/CategoryType.php
800+
namespace Acme\StoreBundle\Form\Type;
801801
802802
use Symfony\Component\Form\AbstractType;
803803
use Symfony\Component\Form\FormBuilder;

cookbook/form/simple_signup_form_with_mongodb.rst

Lines changed: 62 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ How to implement a simple Registration Form with MongoDB
44
Some forms have extra fields whose values don't need to be stored in the
55
database. In this example, we'll create a registration form with some extra
66
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.
88

99
.. tip::
1010

1111
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
1313
how to setup and work with MongoDB inside Symfony.
1414

1515
The simple User model
@@ -40,44 +40,44 @@ So, in this tutorial we begin with the model for a ``User`` document::
4040
* @Assert\NotBlank()
4141
* @Assert\Email()
4242
*/
43-
protected $email;
44-
43+
protected $email;
44+
4545
/**
4646
* @MongoDB\Field(type="string")
4747
* @Assert\NotBlank()
4848
*/
49-
protected $password;
50-
49+
protected $password;
50+
5151
public function getId()
5252
{
53-
return $this->id;
53+
return $this->id;
5454
}
55-
55+
5656
public function getEmail()
5757
{
58-
return $this->email;
58+
return $this->email;
5959
}
60-
60+
6161
public function setEmail($email)
6262
{
63-
$this->email = $email;
63+
$this->email = $email;
6464
}
65-
65+
6666
public function getPassword()
6767
{
68-
return $this->password;
68+
return $this->password;
6969
}
70-
70+
7171
// stupid simple encryption (please don't copy it!)
7272
public function setPassword($password)
7373
{
74-
$this->password = sha1($password);
74+
$this->password = sha1($password);
7575
}
7676
}
7777

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

8282
.. note::
8383

@@ -90,38 +90,38 @@ Create a Form for the Model
9090

9191
Next, create the form for the ``User`` model::
9292

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

9696
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;
9999

100100
class UserType extends AbstractType
101101
{
102102
public function buildForm(FormBuilder $builder, array $options)
103103
{
104-
$builder->add('email', 'email');
104+
$builder->add('email', 'email');
105105
$builder->add('password', 'repeated', array(
106-
'first_name' => 'password',
107-
'second_name' => 'confirm',
106+
'first_name' => 'password',
107+
'second_name' => 'confirm',
108108
'type' => 'password'
109-
));
109+
));
110110
}
111-
111+
112112
public function getDefaultOptions(array $options)
113113
{
114114
return array('data_class' => 'Acme\AccountBundle\Document\User');
115115
}
116116
}
117117

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
119119
password). The ``data_class`` option tells the form the name of data class
120120
(i.e. your ``User`` document).
121121

122122
.. tip::
123123

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>`.
125125

126126
Embedding the User form into a Registration Form
127127
------------------------------------------------
@@ -135,55 +135,55 @@ In other words, create a second form for registration, which embeds the ``User``
135135
form and adds the extra field needed. Start by creating a simple class which
136136
represents the "registration"::
137137

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

141141
use Symfony\Component\Validator\Constraints as Assert;
142142

143143
use Acme\AccountBundle\Document\User;
144144

145145
class Registration
146-
{
146+
{
147147
/**
148148
* @Assert\Type(type="Acme\AccountBundle\Document\User")
149149
*/
150-
protected $user;
151-
150+
protected $user;
151+
152152
/**
153153
* @Assert\NotBlank()
154154
* @Assert\True()
155155
*/
156156
protected $termsAccepted;
157-
157+
158158
public function setUser(User $user)
159159
{
160-
$this->user = $user;
160+
$this->user = $user;
161161
}
162-
162+
163163
public function getUser()
164164
{
165-
return $this->user;
165+
return $this->user;
166166
}
167-
167+
168168
public function getTermsAccepted()
169169
{
170170
return $this->termsAccepted;
171171
}
172-
172+
173173
public function setTermsAccepted($termsAccepted)
174174
{
175-
$this->termsAccepted = (boolean)$termsAccepted;
175+
$this->termsAccepted = (boolean)$termsAccepted;
176176
}
177177
}
178178

179179
Next, create the form for this ``Registration`` model::
180180

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

184184
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;
187187

188188
class RegistrationType extends AbstractType
189189
{
@@ -209,50 +209,53 @@ controller for displaying the registration form::
209209
namespace Acme\AccountBundle\Controller;
210210

211211
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
212-
use Symfony\Component\HttpFoundation\Response;
212+
use Symfony\Component\HttpFoundation\Response;
213213

214-
use Acme\AccountBundle\Form;
214+
use Acme\AccountBundle\Form\Type\RegistrationType;
215+
use Acme\AccountBundle\Form\Model\Registration;
215216

216217
class AccountController extends Controller
217218
{
218219
public function registerAction()
219220
{
220-
$form = $this->createForm(new Form\RegistrationType(), new Form\Registration());
221-
221+
$form = $this->createForm(new RegistrationType(), new Registration());
222+
222223
return $this->render('AcmeAccountBundle:Account:register.html.twig', array('form' => $form->createView()));
223224
}
224225
}
225226

226-
and it's template::
227+
and it's template:
228+
229+
.. code-block:: html+jinja
227230

228231
{# src/Acme/AccountBundle/Resources/views/Account/register.html.twig #}
229232

230233
<form action="{{ path('create')}}" method="post" {{ form_enctype(form) }}>
231234
{{ form_widget(form) }}
232235

233236
<input type="submit" />
234-
</form>
237+
</form>
235238

236239
Finally, create the controller which handles the form submission. This performs
237240
the validation and saves the data into MongoDB::
238241

239242
public function createAction()
240243
{
241244
$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+
247250
if ($form->isValid()) {
248251
$registration = $form->getData();
249-
250-
$dm->persist($registration->getUser());
252+
253+
$dm->persist($registration->getUser());
251254
$dm->flush();
252-
255+
253256
return $this->redirect(...);
254257
}
255-
258+
256259
return $this->render('AcmeAccountBundle:Account:register.html.twig', array('form' => $form->createView()));
257260
}
258261

0 commit comments

Comments
 (0)