@@ -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));
@@ -969,20 +980,21 @@ create a form class so that a ``Category`` object can be modified by the user::
969
980
namespace Acme\TaskBundle\Form\Type;
970
981
971
982
use Symfony\Component\Form\AbstractType;
972
- use Symfony\Component\Form\FormBuilder;
983
+ use Symfony\Component\Form\FormBuilderInterface;
984
+ use Symfony\Component\OptionsResolver\OptionsResolverInterface;
973
985
974
986
class CategoryType extends AbstractType
975
987
{
976
- public function buildForm(FormBuilder $builder, array $options)
988
+ public function buildForm(FormBuilderInterface $builder, array $options)
977
989
{
978
990
$builder->add('name');
979
991
}
980
992
981
- public function getDefaultOptions( )
993
+ public function setDefaultOptions(OptionsResolverInterface $resolver )
982
994
{
983
- return array(
995
+ $resolver->setDefaults( array(
984
996
'data_class' => 'Acme\TaskBundle\Entity\Category',
985
- );
997
+ )) ;
986
998
}
987
999
988
1000
public function getName()
@@ -998,7 +1010,9 @@ class:
998
1010
999
1011
.. code-block :: php
1000
1012
1001
- public function buildForm(FormBuilder $builder, array $options)
1013
+ use Symfony\Component\Form\FormBuilderInterface;
1014
+
1015
+ public function buildForm(FormBuilderInterface $builder, array $options)
1002
1016
{
1003
1017
// ...
1004
1018
@@ -1403,19 +1417,21 @@ that all un-rendered fields are output.
1403
1417
1404
1418
The CSRF token can be customized on a form-by-form basis. For example::
1405
1419
1420
+ use Symfony\Component\OptionsResolver\OptionsResolverInterface;
1421
+
1406
1422
class TaskType extends AbstractType
1407
1423
{
1408
1424
// ...
1409
1425
1410
- public function getDefaultOptions( )
1426
+ public function setDefaultOptions(OptionsResolverInterface $resolver )
1411
1427
{
1412
- return array(
1428
+ $resolver->setDefaults( array(
1413
1429
'data_class' => 'Acme\TaskBundle\Entity\Task',
1414
1430
'csrf_protection' => true,
1415
1431
'csrf_field_name' => '_token',
1416
1432
// a unique key to help generate the secret token
1417
1433
'intention' => 'task_item',
1418
- );
1434
+ )) ;
1419
1435
}
1420
1436
1421
1437
// ...
@@ -1531,6 +1547,7 @@ method to specify the option::
1531
1547
1532
1548
use Symfony\Component\Form\AbstractType;
1533
1549
use Symfony\Component\Form\FormBuilder;
1550
+ use Symfony\Component\OptionsResolver\OptionsResolverInterface;
1534
1551
use Symfony\Component\Validator\Constraints\Email;
1535
1552
use Symfony\Component\Validator\Constraints\MinLength;
1536
1553
use Symfony\Component\Validator\Constraints\Collection;
@@ -1539,14 +1556,16 @@ method to specify the option::
1539
1556
{
1540
1557
// ...
1541
1558
1542
- public function getDefaultOptions( )
1559
+ public function setDefaultOptions(OptionsResolverInterface $resolver )
1543
1560
{
1544
1561
$collectionConstraint = new Collection(array(
1545
1562
'name' => new MinLength(5),
1546
1563
'email' => new Email(array('message' => 'Invalid email address')),
1547
1564
));
1548
1565
1549
- return array('validation_constraint' => $collectionConstraint);
1566
+ $resolver->setDefaults(array(
1567
+ 'validation_constraint' => $collectionConstraint
1568
+ ));
1550
1569
}
1551
1570
}
1552
1571
0 commit comments