@@ -9,7 +9,7 @@ The Form Component
9
9
forms.
10
10
11
11
The form component is a tool to help you solve the problem of allowing end-users
12
- to interact with the data and modify the data in your application. And thought
12
+ to interact with the data and modify the data in your application. And though
13
13
traditionally this has been through HTML forms, the component focuses on
14
14
processing data to and from your client and application, whether that data
15
15
be from a normal form post or from an API.
@@ -155,7 +155,7 @@ line to your ``composer.json`` file:
155
155
}
156
156
}
157
157
158
- The TwigBridge integration provides you with several :doc: `Twig Functions</reference/forms/twig_reference `
158
+ The TwigBridge integration provides you with several :doc: `Twig Functions</reference/forms/twig_reference> `
159
159
that help you render each the HTML widget, label and error for each field
160
160
(as well as a few other things). To configure the integration, you'll need
161
161
to bootstrap or access Twig and add the :class: `Symfony\\ Bridge\\ Twig\\ Extension\\ FormExtension `::
@@ -267,8 +267,8 @@ Validation
267
267
268
268
The Form component comes with tight (but optional) integration with Symfony's
269
269
Validator component. If you're using a different solution for validation,
270
- no problem! Simply take the bound data of your form (which is an array or
271
- object) and pass it through your own validation system.
270
+ no problem! Simply take the submitted/ bound data of your form (which is an
271
+ array or object) and pass it through your own validation system.
272
272
273
273
To use the integration with Symfony's Validator component, first make sure
274
274
it's installed in your application. If you're using Composer and want to
@@ -302,8 +302,18 @@ Your integration with the Validation component will look something like this::
302
302
$validator = Validation::createValidator();
303
303
304
304
// there are built-in translations for the core error messages
305
- $translator->addResource('xlf', VENDOR_FORM_DIR . '/Resources/translations/validators.en.xlf', 'en', 'validators');
306
- $translator->addResource('xlf', VENDOR_VALIDATOR_DIR . '/Resources/translations/validators.en.xlf', 'en', 'validators');
305
+ $translator->addResource(
306
+ 'xlf',
307
+ VENDOR_FORM_DIR . '/Resources/translations/validators.en.xlf',
308
+ 'en',
309
+ 'validators'
310
+ );
311
+ $translator->addResource(
312
+ 'xlf',
313
+ VENDOR_VALIDATOR_DIR . '/Resources/translations/validators.en.xlf',
314
+ 'en',
315
+ 'validators'
316
+ );
307
317
308
318
$formFactory = Forms::createFormFactoryBuilder()
309
319
// ...
@@ -320,6 +330,12 @@ should be used to create any and all form objects in your application. This
320
330
means that you should create it in some central, bootstrap part of your application
321
331
and then access it whenever you need to build a form.
322
332
333
+ .. note ::
334
+
335
+ In this document, the form factory is always a locally variable called
336
+ ``$formFactory ``. The point here is that you will probably need to create
337
+ this object in some more "global" way so you can access it from anywhere.
338
+
323
339
Exactly how you gain access to your one form factory is up to you. If you're
324
340
using a :term`Service Container`, then you should add the form factory to
325
341
your container and grab it out whenever you need to. If your application
@@ -373,7 +389,7 @@ is created from the form factory.
373
389
public function newAction(Request $request)
374
390
{
375
391
// createFormBuilder is a shortcut to get the "form factory"
376
- // and then call "createBuilder" on it
392
+ // and then call "createBuilder() " on it
377
393
$form = $this->createFormBuilder()
378
394
->add('task', 'text')
379
395
->add('dueDate', 'date')
@@ -388,7 +404,7 @@ is created from the form factory.
388
404
As you can see, creating a form is like writing a recipe: you call ``add ``
389
405
for each new field you want to create. The first argument to ``add `` is the
390
406
name of your field, and the second is the field "type". The Form component
391
- comes with a lot of :ref : `built-in types</reference/forms/types> `.
407
+ comes with a lot of :doc : `built-in types</reference/forms/types> `.
392
408
393
409
Now that you've built your form, learn how to :ref: `render<component-form-intro-rendering-form> `
394
410
it and :ref: `process the form submission<component-form-intro-handling-submission> `.
@@ -507,6 +523,7 @@ method:
507
523
->add('dueDate', 'date')
508
524
->getForm();
509
525
526
+ // only process the form if the request is a POST request
510
527
if ($request->isMethod('POST')) {
511
528
$form->bind($request);
512
529
@@ -522,17 +539,20 @@ method:
522
539
// ...
523
540
}
524
541
525
- This defines a common form "workflow", which looks like this:
542
+ This defines a common form "workflow", which contains 3 different possibilities:
543
+
544
+ 1) On the initial GET request (i.e. when the user "surfs" to your page),
545
+ build your form and render it;
546
+
547
+ If the request is a POST, process the submitted data (via ``bind ``). Then:
526
548
527
- 1) Build your form;
528
- 2) If POST, process the form by calling ``bind ``;
529
- 3a) If the form is valid, perform some action and redirect;
530
- 3b) If the form is invalid, re-render the form (which will now contain errors)
549
+ 2) if the form is invalid, re-render the form (which will now contain errors)
550
+ 3) if the form is valid, perform some action and redirect;
531
551
532
552
.. note ::
533
553
534
554
If you're not using HttpFoundation, just pass the POST'ed data directly
535
- to ``bind ``:
555
+ to ``bind ``::
536
556
537
557
if (isset($_POST[$form->getName()])) {
538
558
$form->bind($_POST[$form->getName())
@@ -598,3 +618,4 @@ and the errors will display next to the fields on error.
598
618
599
619
.. _Packagist : https://packagist.org/packages/symfony/form
600
620
.. _Twig : http://twig.sensiolabs.org
621
+ .. _`Twig Configuration` : http://twig.sensiolabs.org/doc/intro.html
0 commit comments