Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e4f41e6

Browse filesBrowse files
committed
Merge pull request symfony#1425 from nomack84/form_updates
[Form] Update the Form related documentation
2 parents cd33473 + b41e8b9 commit e4f41e6
Copy full SHA for e4f41e6

File tree

Expand file treeCollapse file tree

6 files changed

+105
-74
lines changed
Filter options
Expand file treeCollapse file tree

6 files changed

+105
-74
lines changed

‎book/forms.rst

Copy file name to clipboardExpand all lines: book/forms.rst
+43-24Lines 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));
@@ -973,20 +984,21 @@ create a form class so that a ``Category`` object can be modified by the user::
973984
namespace Acme\TaskBundle\Form\Type;
974985

975986
use Symfony\Component\Form\AbstractType;
976-
use Symfony\Component\Form\FormBuilder;
987+
use Symfony\Component\Form\FormBuilderInterface;
988+
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
977989

978990
class CategoryType extends AbstractType
979991
{
980-
public function buildForm(FormBuilder $builder, array $options)
992+
public function buildForm(FormBuilderInterface $builder, array $options)
981993
{
982994
$builder->add('name');
983995
}
984996

985-
public function getDefaultOptions()
997+
public function setDefaultOptions(OptionsResolverInterface $resolver)
986998
{
987-
return array(
999+
$resolver->setDefaults(array(
9881000
'data_class' => 'Acme\TaskBundle\Entity\Category',
989-
);
1001+
));
9901002
}
9911003

9921004
public function getName()
@@ -1002,7 +1014,9 @@ class:
10021014

10031015
.. code-block:: php
10041016
1005-
public function buildForm(FormBuilder $builder, array $options)
1017+
use Symfony\Component\Form\FormBuilderInterface;
1018+
1019+
public function buildForm(FormBuilderInterface $builder, array $options)
10061020
{
10071021
// ...
10081022
@@ -1413,19 +1427,21 @@ that all un-rendered fields are output.
14131427

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

1430+
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
1431+
14161432
class TaskType extends AbstractType
14171433
{
14181434
// ...
14191435

1420-
public function getDefaultOptions()
1436+
public function setDefaultOptions(OptionsResolverInterface $resolver)
14211437
{
1422-
return array(
1438+
$resolver->setDefaults(array(
14231439
'data_class' => 'Acme\TaskBundle\Entity\Task',
14241440
'csrf_protection' => true,
14251441
'csrf_field_name' => '_token',
14261442
// a unique key to help generate the secret token
14271443
'intention' => 'task_item',
1428-
);
1444+
));
14291445
}
14301446

14311447
// ...
@@ -1541,6 +1557,7 @@ method to specify the option::
15411557

15421558
use Symfony\Component\Form\AbstractType;
15431559
use Symfony\Component\Form\FormBuilder;
1560+
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
15441561
use Symfony\Component\Validator\Constraints\Email;
15451562
use Symfony\Component\Validator\Constraints\MinLength;
15461563
use Symfony\Component\Validator\Constraints\Collection;
@@ -1549,14 +1566,16 @@ method to specify the option::
15491566
{
15501567
// ...
15511568

1552-
public function getDefaultOptions()
1569+
public function setDefaultOptions(OptionsResolverInterface $resolver)
15531570
{
15541571
$collectionConstraint = new Collection(array(
15551572
'name' => new MinLength(5),
15561573
'email' => new Email(array('message' => 'Invalid email address')),
15571574
));
15581575

1559-
return array('validation_constraint' => $collectionConstraint);
1576+
$resolver->setDefaults(array(
1577+
'validation_constraint' => $collectionConstraint
1578+
));
15601579
}
15611580
}
15621581

‎cookbook/form/create_custom_field_type.rst

Copy file name to clipboardExpand all lines: cookbook/form/create_custom_field_type.rst
+20-14Lines 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

Copy file name to clipboardExpand all lines: cookbook/form/data_transformers.rst
+10-15Lines 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

Copy file name to clipboardExpand all lines: cookbook/form/dynamic_form_generation.rst
+4-4Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.