@@ -87,7 +87,7 @@ The standard pattern for using a form in a controller looks like this:
87
87
public function contactAction()
88
88
{
89
89
$contactRequest = new ContactRequest();
90
- $form = new ContactForm( );
90
+ $form = new ContactForm::create($this->get('form.context') );
91
91
92
92
// If a POST request, write submitted data into $contactRequest
93
93
// and validate it
@@ -110,6 +110,23 @@ There are two code paths there:
110
110
the template.
111
111
2. If the form has been submitted and is valid, the contact request is sent.
112
112
113
+ We created the form with the static ``create() `` method. This method expects
114
+ a form context that contains all default services (for example a ``Validator ``)
115
+ and settings that a form needs to work.
116
+
117
+ .. note:
118
+
119
+ If you don't use Symfony2 or its service container, don't worry. You can
120
+ easily create a ``FormContext`` and a ``Request`` manually:
121
+
122
+ .. code-block:: php
123
+
124
+ use Symfony\Component\Form\FormContext
125
+ use Symfony\Component\HttpFoundation\Request
126
+
127
+ $context = FormContext::buildDefault();
128
+ $request = Request::createFromGlobals();
129
+
113
130
Forms and domain objects
114
131
------------------------
115
132
@@ -179,8 +196,97 @@ Validating submitted data
179
196
180
197
The form uses the ``Validator `` component to validate submitted form values.
181
198
All constraints on the domain object, on the form and on its fields will be
182
- validated when ``bind() `` is called. You can learn more about constraints
183
- in :doc: `Validation constraints </guides/validator/constraints >`.
199
+ validated when ``bind() `` is called. We will add a few constraints to
200
+ ``ContactRequest `` to make sure that nobody can submit the form with invalid
201
+ data.
202
+
203
+ .. code-block :: php
204
+
205
+ // src/Sensio/HelloBundle/Contact/ContactRequest.php
206
+ class ContactRequest
207
+ {
208
+ /**
209
+ * @validation:MaxLength(100)
210
+ * @validation:NotBlank
211
+ */
212
+ protected $subject = 'Subject...';
213
+
214
+ /**
215
+ * @validation:NotBlank
216
+ */
217
+ protected $message;
218
+
219
+ /**
220
+ * @validation:Email
221
+ * @validation:NotBlank
222
+ */
223
+ protected $sender;
224
+
225
+ /**
226
+ * @validation:AssertType("boolean")
227
+ */
228
+ protected $ccmyself = false;
229
+
230
+ // Other code...
231
+ }
232
+
233
+ If any constraint fails, the error is displayed next to the corresponding
234
+ form field. You can learn more about constraints in :doc: `Validation
235
+ constraints </guides/validator/constraints>`.
236
+
237
+ Creating form fields automatically
238
+ ----------------------------------
239
+
240
+ If you use Doctrine 2 or Symfony's ``Validator ``, Symfony already knows quite
241
+ a lot about your domain classes. It knows which data type is used to persist
242
+ a property in the database, what validation constraints the property has etc.
243
+ The Form component can use this information to "guess" which field type should
244
+ be created with which settings.
245
+
246
+ To use this feature, a form needs to know the class of the related domain
247
+ object. You can set this class within the ``configure() `` method of the form.
248
+ Calling ``add() `` with only the name of the property will then automatically
249
+ create the best-matching field.
250
+
251
+ .. code-block :: php
252
+
253
+ // src/Sensio/HelloBundle/Contact/ContactForm.php
254
+ class ContactForm extends Form
255
+ {
256
+ protected function configure()
257
+ {
258
+ $this->add('subject'); // TextField with max_length=100 because
259
+ // of the @MaxLength constraint
260
+ $this->add('message'); // TextField
261
+ $this->add('sender'); // EmailField because of the @Email constraint
262
+ $this->add('ccmyself'); // CheckboxField because of @AssertType("boolean")
263
+ }
264
+ }
265
+
266
+ These field guesses are obviously not always right. For the property ``message ``
267
+ Symfony created a ``TextField ``, it couldn't know from the validation constraints
268
+ that you wanted a ``TextareaField `` instead. So you have to create this field
269
+ manually. You can also tweak the options of the generated fields by passing
270
+ them in the second parameter. We will add a ``max_length `` option to the
271
+ ``sender `` field to limit its length.
272
+
273
+ .. code-block :: php
274
+
275
+ // src/Sensio/HelloBundle/Contact/ContactForm.php
276
+ class ContactForm extends Form
277
+ {
278
+ protected function configure()
279
+ {
280
+ $this->add('subject');
281
+ $this->add(new TextareaField('message'));
282
+ $this->add('sender', array('max_length' => 50));
283
+ $this->add('ccmyself');
284
+ }
285
+ }
286
+
287
+ Generating form fields automatically helps you to improve your development
288
+ speed and reduces code duplication. You can store information about class
289
+ properties once and let Symfony2 do the work for you.
184
290
185
291
Rendering forms as HTML
186
292
-----------------------
0 commit comments