@@ -393,11 +393,13 @@ If you're creating :ref:`form classes<book-form-creating-form-classes>` (a
393
393
good practice), then you'll need to add the following to the ``getDefaultOptions() ``
394
394
method::
395
395
396
- public function getDefaultOptions()
396
+ use Symfony\Component\OptionsResolver\OptionsResolverInterface;
397
+
398
+ public function setDefaultOptions(OptionsResolverInterface $resolver)
397
399
{
398
- return array(
400
+ $resolver->setDefaults( array(
399
401
'validation_groups' => array('registration')
400
- );
402
+ )) ;
401
403
}
402
404
403
405
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.
414
416
based on submitted data), you can set the ``validation_groups `` option
415
417
to an array callback, or a ``Closure ``::
416
418
417
- public function getDefaultOptions()
419
+ use Symfony\Component\OptionsResolver\OptionsResolverInterface;
420
+
421
+ public function setDefaultOptions(OptionsResolverInterface $resolver)
418
422
{
419
- return array(
423
+ $resolver->setDefaults( array(
420
424
'validation_groups' => array('Acme\\AcmeBundle\\Entity\\Client', 'determineValidationGroups'),
421
- );
425
+ )) ;
422
426
}
423
427
424
428
This will call the static method ``determineValidationGroups() `` on the
425
429
``Client `` class after the form is bound, but before validation is executed.
426
430
The Form object is passed as an argument to that method (see next example).
427
431
You can also define whole logic inline by using a Closure::
428
432
429
- public function getDefaultOptions()
433
+ use Symfony\Component\Form\FormInterface;
434
+ use Symfony\Component\OptionsResolver\OptionsResolverInterface;
435
+
436
+ public function setDefaultOptions(OptionsResolverInterface $resolver)
430
437
{
431
- return array(
438
+ $resolver->setDefaults( array(
432
439
'validation_groups' => function(FormInterface $form) {
433
440
$data = $form->getData();
434
441
if (Entity\Client::TYPE_PERSON == $data->getType()) {
@@ -437,7 +444,7 @@ You can also define whole logic inline by using a Closure::
437
444
return array('company');
438
445
}
439
446
},
440
- );
447
+ )) ;
441
448
}
442
449
443
450
.. index ::
@@ -846,11 +853,13 @@ the choice is ultimately up to you.
846
853
good idea to explicitly specify the ``data_class `` option by adding the
847
854
following to your form type class::
848
855
849
- public function getDefaultOptions()
856
+ use Symfony\Component\OptionsResolver\OptionsResolverInterface;
857
+
858
+ public function setDefaultOptions(OptionsResolverInterface $resolver)
850
859
{
851
- return array(
860
+ $resolver->setDefaults( array(
852
861
'data_class' => 'Acme\TaskBundle\Entity\Task',
853
- );
862
+ )) ;
854
863
}
855
864
856
865
.. tip ::
@@ -863,7 +872,9 @@ the choice is ultimately up to you.
863
872
agree with these terms" checkbox) that will not be mapped to the underlying
864
873
object, you need to set the property_path option to ``false ``::
865
874
866
- public function buildForm(FormBuilder $builder, array $options)
875
+ use Symfony\Component\Form\FormBuilderInterface;
876
+
877
+ public function buildForm(FormBuilderInterface $builder, array $options)
867
878
{
868
879
$builder->add('task');
869
880
$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::
973
984
namespace Acme\TaskBundle\Form\Type;
974
985
975
986
use Symfony\Component\Form\AbstractType;
976
- use Symfony\Component\Form\FormBuilder;
987
+ use Symfony\Component\Form\FormBuilderInterface;
988
+ use Symfony\Component\OptionsResolver\OptionsResolverInterface;
977
989
978
990
class CategoryType extends AbstractType
979
991
{
980
- public function buildForm(FormBuilder $builder, array $options)
992
+ public function buildForm(FormBuilderInterface $builder, array $options)
981
993
{
982
994
$builder->add('name');
983
995
}
984
996
985
- public function getDefaultOptions( )
997
+ public function setDefaultOptions(OptionsResolverInterface $resolver )
986
998
{
987
- return array(
999
+ $resolver->setDefaults( array(
988
1000
'data_class' => 'Acme\TaskBundle\Entity\Category',
989
- );
1001
+ )) ;
990
1002
}
991
1003
992
1004
public function getName()
@@ -1002,7 +1014,9 @@ class:
1002
1014
1003
1015
.. code-block :: php
1004
1016
1005
- public function buildForm(FormBuilder $builder, array $options)
1017
+ use Symfony\Component\Form\FormBuilderInterface;
1018
+
1019
+ public function buildForm(FormBuilderInterface $builder, array $options)
1006
1020
{
1007
1021
// ...
1008
1022
@@ -1413,19 +1427,21 @@ that all un-rendered fields are output.
1413
1427
1414
1428
The CSRF token can be customized on a form-by-form basis. For example::
1415
1429
1430
+ use Symfony\Component\OptionsResolver\OptionsResolverInterface;
1431
+
1416
1432
class TaskType extends AbstractType
1417
1433
{
1418
1434
// ...
1419
1435
1420
- public function getDefaultOptions( )
1436
+ public function setDefaultOptions(OptionsResolverInterface $resolver )
1421
1437
{
1422
- return array(
1438
+ $resolver->setDefaults( array(
1423
1439
'data_class' => 'Acme\TaskBundle\Entity\Task',
1424
1440
'csrf_protection' => true,
1425
1441
'csrf_field_name' => '_token',
1426
1442
// a unique key to help generate the secret token
1427
1443
'intention' => 'task_item',
1428
- );
1444
+ )) ;
1429
1445
}
1430
1446
1431
1447
// ...
@@ -1541,6 +1557,7 @@ method to specify the option::
1541
1557
1542
1558
use Symfony\Component\Form\AbstractType;
1543
1559
use Symfony\Component\Form\FormBuilder;
1560
+ use Symfony\Component\OptionsResolver\OptionsResolverInterface;
1544
1561
use Symfony\Component\Validator\Constraints\Email;
1545
1562
use Symfony\Component\Validator\Constraints\MinLength;
1546
1563
use Symfony\Component\Validator\Constraints\Collection;
@@ -1549,14 +1566,16 @@ method to specify the option::
1549
1566
{
1550
1567
// ...
1551
1568
1552
- public function getDefaultOptions( )
1569
+ public function setDefaultOptions(OptionsResolverInterface $resolver )
1553
1570
{
1554
1571
$collectionConstraint = new Collection(array(
1555
1572
'name' => new MinLength(5),
1556
1573
'email' => new Email(array('message' => 'Invalid email address')),
1557
1574
));
1558
1575
1559
- return array('validation_constraint' => $collectionConstraint);
1576
+ $resolver->setDefaults(array(
1577
+ 'validation_constraint' => $collectionConstraint
1578
+ ));
1560
1579
}
1561
1580
}
1562
1581
0 commit comments