Skip to content

Commit b41e8b9

Browse files
author
nomack84
committed
[Form] Update the Form related documentation
1 parent c095ebd commit b41e8b9

File tree

6 files changed

+105
-74
lines changed

6 files changed

+105
-74
lines changed

book/forms.rst

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -393,11 +393,13 @@ If you're creating :ref:`form classes<book-form-creating-form-classes>` (a
393393
good practice), then you'll need to add the following to the ``getDefaultOptions()``
394394
method::
395395

396-
public function getDefaultOptions()
396+
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
397+
398+
public function setDefaultOptions(OptionsResolverInterface $resolver)
397399
{
398-
return array(
400+
$resolver->setDefaults(array(
399401
'validation_groups' => array('registration')
400-
);
402+
));
401403
}
402404

403405
In both of these cases, *only* the ``registration`` validation group will
@@ -414,21 +416,26 @@ If you need some advanced logic to determine the validation groups (e.g.
414416
based on submitted data), you can set the ``validation_groups`` option
415417
to an array callback, or a ``Closure``::
416418

417-
public function getDefaultOptions()
419+
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
420+
421+
public function setDefaultOptions(OptionsResolverInterface $resolver)
418422
{
419-
return array(
423+
$resolver->setDefaults(array(
420424
'validation_groups' => array('Acme\\AcmeBundle\\Entity\\Client', 'determineValidationGroups'),
421-
);
425+
));
422426
}
423427

424428
This will call the static method ``determineValidationGroups()`` on the
425429
``Client`` class after the form is bound, but before validation is executed.
426430
The Form object is passed as an argument to that method (see next example).
427431
You can also define whole logic inline by using a Closure::
428432

429-
public function getDefaultOptions()
433+
use Symfony\Component\Form\FormInterface;
434+
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
435+
436+
public function setDefaultOptions(OptionsResolverInterface $resolver)
430437
{
431-
return array(
438+
$resolver->setDefaults(array(
432439
'validation_groups' => function(FormInterface $form) {
433440
$data = $form->getData();
434441
if (Entity\Client::TYPE_PERSON == $data->getType()) {
@@ -437,7 +444,7 @@ You can also define whole logic inline by using a Closure::
437444
return array('company');
438445
}
439446
},
440-
);
447+
));
441448
}
442449

443450
.. index::
@@ -846,11 +853,13 @@ the choice is ultimately up to you.
846853
good idea to explicitly specify the ``data_class`` option by adding the
847854
following to your form type class::
848855

849-
public function getDefaultOptions()
856+
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
857+
858+
public function setDefaultOptions(OptionsResolverInterface $resolver)
850859
{
851-
return array(
860+
$resolver->setDefaults(array(
852861
'data_class' => 'Acme\TaskBundle\Entity\Task',
853-
);
862+
));
854863
}
855864

856865
.. tip::
@@ -863,7 +872,9 @@ the choice is ultimately up to you.
863872
agree with these terms" checkbox) that will not be mapped to the underlying
864873
object, you need to set the property_path option to ``false``::
865874

866-
public function buildForm(FormBuilder $builder, array $options)
875+
use Symfony\Component\Form\FormBuilderInterface;
876+
877+
public function buildForm(FormBuilderInterface $builder, array $options)
867878
{
868879
$builder->add('task');
869880
$builder->add('dueDate', null, array('property_path' => false));
@@ -969,20 +980,21 @@ create a form class so that a ``Category`` object can be modified by the user::
969980
namespace Acme\TaskBundle\Form\Type;
970981

971982
use Symfony\Component\Form\AbstractType;
972-
use Symfony\Component\Form\FormBuilder;
983+
use Symfony\Component\Form\FormBuilderInterface;
984+
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
973985

974986
class CategoryType extends AbstractType
975987
{
976-
public function buildForm(FormBuilder $builder, array $options)
988+
public function buildForm(FormBuilderInterface $builder, array $options)
977989
{
978990
$builder->add('name');
979991
}
980992

981-
public function getDefaultOptions()
993+
public function setDefaultOptions(OptionsResolverInterface $resolver)
982994
{
983-
return array(
995+
$resolver->setDefaults(array(
984996
'data_class' => 'Acme\TaskBundle\Entity\Category',
985-
);
997+
));
986998
}
987999

9881000
public function getName()
@@ -998,7 +1010,9 @@ class:
9981010

9991011
.. code-block:: php
10001012
1001-
public function buildForm(FormBuilder $builder, array $options)
1013+
use Symfony\Component\Form\FormBuilderInterface;
1014+
1015+
public function buildForm(FormBuilderInterface $builder, array $options)
10021016
{
10031017
// ...
10041018
@@ -1403,19 +1417,21 @@ that all un-rendered fields are output.
14031417

14041418
The CSRF token can be customized on a form-by-form basis. For example::
14051419

1420+
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
1421+
14061422
class TaskType extends AbstractType
14071423
{
14081424
// ...
14091425

1410-
public function getDefaultOptions()
1426+
public function setDefaultOptions(OptionsResolverInterface $resolver)
14111427
{
1412-
return array(
1428+
$resolver->setDefaults(array(
14131429
'data_class' => 'Acme\TaskBundle\Entity\Task',
14141430
'csrf_protection' => true,
14151431
'csrf_field_name' => '_token',
14161432
// a unique key to help generate the secret token
14171433
'intention' => 'task_item',
1418-
);
1434+
));
14191435
}
14201436

14211437
// ...
@@ -1531,6 +1547,7 @@ method to specify the option::
15311547

15321548
use Symfony\Component\Form\AbstractType;
15331549
use Symfony\Component\Form\FormBuilder;
1550+
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
15341551
use Symfony\Component\Validator\Constraints\Email;
15351552
use Symfony\Component\Validator\Constraints\MinLength;
15361553
use Symfony\Component\Validator\Constraints\Collection;
@@ -1539,14 +1556,16 @@ method to specify the option::
15391556
{
15401557
// ...
15411558

1542-
public function getDefaultOptions()
1559+
public function setDefaultOptions(OptionsResolverInterface $resolver)
15431560
{
15441561
$collectionConstraint = new Collection(array(
15451562
'name' => new MinLength(5),
15461563
'email' => new Email(array('message' => 'Invalid email address')),
15471564
));
15481565

1549-
return array('validation_constraint' => $collectionConstraint);
1566+
$resolver->setDefaults(array(
1567+
'validation_constraint' => $collectionConstraint
1568+
));
15501569
}
15511570
}
15521571

cookbook/form/create_custom_field_type.rst

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,21 @@ for form fields, which is ``<BundleName>\Form\Type``. Make sure the field extend
2424
namespace Acme\DemoBundle\Form\Type;
2525

2626
use Symfony\Component\Form\AbstractType;
27-
use Symfony\Component\Form\FormBuilder;
27+
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
2828

2929
class GenderType extends AbstractType
3030
{
31-
public function getDefaultOptions()
31+
public function setDefaultOptions(OptionsResolverInterface $resolver)
3232
{
33-
return array(
33+
$resolver->setDefaults(array(
3434
'choices' => array(
3535
'm' => 'Male',
3636
'f' => 'Female',
3737
)
38-
);
38+
));
3939
}
4040

41-
public function getParent(array $options)
41+
public function getParent()
4242
{
4343
return 'choice';
4444
}
@@ -152,11 +152,11 @@ new instance of the type in one of your forms::
152152
namespace Acme\DemoBundle\Form\Type;
153153

154154
use Symfony\Component\Form\AbstractType;
155-
use Symfony\Component\Form\FormBuilder;
155+
use Symfony\Component\Form\FormBuilderInterface;
156156
157157
class AuthorType extends AbstractType
158158
{
159-
public function buildForm(FormBuilder $builder, array $options)
159+
public function buildForm(FormBuilderInterface $builder, array $options)
160160
{
161161
$builder->add('gender_code', new GenderType(), array(
162162
'empty_value' => 'Choose a gender',
@@ -233,6 +233,9 @@ argument to ``GenderType``, which receives the gender configuration::
233233

234234
# src/Acme/DemoBundle/Form/Type/GenderType.php
235235
namespace Acme\DemoBundle\Form\Type;
236+
237+
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
238+
236239
// ...
237240

238241
class GenderType extends AbstractType
@@ -244,13 +247,13 @@ argument to ``GenderType``, which receives the gender configuration::
244247
$this->genderChoices = $genderChoices;
245248
}
246249
247-
public function getDefaultOptions()
250+
public function setDefaultOptions(OptionsResolverInterface $resolver)
248251
{
249-
return array(
250-
'choices' => $this->genderChoices,
251-
);
252+
$resolver->setDefaults(array(
253+
'data_class' => $this->genderChoices
254+
));
252255
}
253-
256+
254257
// ...
255258
}
256259

@@ -260,11 +263,14 @@ configuration, using the field is now much easier::
260263

261264
// src/Acme/DemoBundle/Form/Type/AuthorType.php
262265
namespace Acme\DemoBundle\Form\Type;
266+
267+
use Symfony\Component\Form\FormBuilderInterface;
268+
263269
// ...
264270

265271
class AuthorType extends AbstractType
266272
{
267-
public function buildForm(FormBuilder $builder, array $options)
273+
public function buildForm(FormBuilderInterface $builder, array $options)
268274
{
269275
$builder->add('gender_code', 'gender', array(
270276
'empty_value' => 'Choose a gender',
@@ -276,4 +282,4 @@ Notice that instead of instantiating a new instance, we can just refer to
276282
it by the alias used in our service configuration, ``gender``. Have fun!
277283

278284
.. _`ChoiceType`: https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php
279-
.. _`FieldType`: https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/Extension/Core/Type/FieldType.php
285+
.. _`FieldType`: https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/Extension/Core/Type/FieldType.php

cookbook/form/data_transformers.rst

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ was entered::
2525
namespace Acme\TaskBundle\Form\Type;
2626

2727
use Symfony\Component\Form\AbstractType;
28-
use Symfony\Component\Form\FormBuilder;
28+
use Symfony\Component\Form\FormBuilderInterface;
29+
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
2930
use Acme\TaskBundle\Form\DataTransformer\IssueToNumberTransformer;
3031
use Doctrine\Common\Persistence\ObjectManager;
3132

@@ -44,20 +45,13 @@ was entered::
4445
$this->om = $om;
4546
}
4647

47-
public function buildForm(FormBuilder $builder, array $options)
48+
public function buildForm(FormBuilderInterface $builder, array $options)
4849
{
4950
$transformer = new IssueToNumberTransformer($this->om);
50-
$builder->appendClientTransformer($transformer);
51+
$builder->addViewTransformer($transformer);
5152
}
5253

53-
public function getDefaultOptions()
54-
{
55-
return array(
56-
'invalid_message' => 'The selected issue does not exist',
57-
);
58-
}
59-
60-
public function getParent(array $options)
54+
public function getParent()
6155
{
6256
return 'text';
6357
}
@@ -73,11 +67,12 @@ was entered::
7367
You can also use transformers without creating a new custom form type
7468
by calling ``appendClientTransformer`` on any field builder::
7569

70+
use Symfony\Component\Form\FormBuilderInterface;
7671
use Acme\TaskBundle\Form\DataTransformer\IssueToNumberTransformer;
7772

7873
class TaskType extends AbstractType
7974
{
80-
public function buildForm(FormBuilder $builder, array $options)
75+
public function buildForm(FormBuilderInterface $builder, array $options)
8176
{
8277
// ...
8378

@@ -88,7 +83,7 @@ was entered::
8883
// use a normal text field, but transform the text into an issue object
8984
$builder
9085
->add('issue', 'text')
91-
->appendClientTransformer($transformer)
86+
->addViewTransformer($transformer)
9287
;
9388
}
9489

@@ -194,11 +189,11 @@ You can now add the type to your form by its alias as follows::
194189
namespace Acme\TaskBundle\Form\Type;
195190

196191
use Symfony\Component\Form\AbstractType;
197-
use Symfony\Component\Form\FormBuilder;
192+
use Symfony\Component\Form\FormBuilderInterface;
198193

199194
class TaskType extends AbstractType
200195
{
201-
public function buildForm(FormBuilder $builder, array $options)
196+
public function buildForm(FormBuilderInterface $builder, array $options)
202197
{
203198
$builder
204199
->add('task')

cookbook/form/dynamic_form_generation.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ of what a bare form class looks like::
1111
namespace Acme\DemoBundle\Form;
1212

1313
use Symfony\Component\Form\AbstractType;
14-
use Symfony\Component\Form\FormBuilder;
14+
use Symfony\Component\Form\FormBuilderInterface;
1515
1616
class ProductType extends AbstractType
1717
{
18-
public function buildForm(FormBuilder $builder, array $options)
18+
public function buildForm(FormBuilderInterface $builder, array $options)
1919
{
2020
$builder->add('name');
2121
$builder->add('price');
@@ -57,12 +57,12 @@ to an Event Subscriber::
5757
namespace Acme\DemoBundle\Form
5858

5959
use Symfony\Component\Form\AbstractType
60-
use Symfony\Component\Form\FormBuilder;
60+
use Symfony\Component\Form\FormBuilderInterface;
6161
use Acme\DemoBundle\Form\EventListener\AddNameFieldSubscriber;
6262

6363
class ProductType extends AbstractType
6464
{
65-
public function buildForm(FormBuilder $builder, array $options)
65+
public function buildForm(FormBuilderInterface $builder, array $options)
6666
{
6767
$subscriber = new AddNameFieldSubscriber($builder->getFormFactory());
6868
$builder->addEventSubscriber($subscriber);

0 commit comments

Comments
 (0)