@@ -833,78 +833,6 @@ form "type"). It can be used to quickly build a form object in the controller::
833
833
// ...
834
834
}
835
835
836
- .. tip ::
837
-
838
- Defining your form type as a service is a good practice and makes it easily usable in
839
- your application:
840
-
841
- .. configuration-block ::
842
-
843
- .. code-block :: yaml
844
-
845
- # src/Acme/TaskBundle/Resources/config/services.yml
846
- services :
847
- acme_demo.form.type.task :
848
- class : Acme\TaskBundle\Form\Type\TaskType
849
- tags :
850
- - { name: form.type, alias: task }
851
-
852
- .. code-block :: xml
853
-
854
- <!-- src/Acme/TaskBundle/Resources/config/services.xml -->
855
- <service id =" acme_demo.form.type.task" class =" Acme\TaskBundle\Form\Type\TaskType" >
856
- <tag name =" form.type" alias =" task" />
857
- </service >
858
-
859
- .. code-block :: php
860
-
861
- // src/Acme/TaskBundle/Resources/config/services.php
862
- use Symfony\Component\DependencyInjection\Definition;
863
-
864
- $container
865
- ->register('acme_demo.form.type.task', 'Acme\TaskBundle\Form\Type\TaskType')
866
- ->addTag('form.type', array(
867
- 'alias' => 'task',
868
- ))
869
- ;
870
-
871
- That's it! Now you can use your form type directly in a controller::
872
-
873
- // src/Acme/TaskBundle/Controller/DefaultController.php
874
-
875
- // ...
876
- public function newAction()
877
- {
878
- $task = ...;
879
- $form = $this->createForm('task', $task);
880
-
881
- // ...
882
- }
883
-
884
- or even use it as a normal type in another form::
885
-
886
- // src/Acme/TaskBundle/Form/Type/ListType.php
887
- namespace Acme\TaskBundle\Form\Type;
888
-
889
- use Symfony\Component\Form\AbstractType;
890
- use Symfony\Component\Form\FormBuilderInterface;
891
-
892
- class ListType extends AbstractType
893
- {
894
- public function buildForm(FormBuilderInterface $builder, array $options)
895
- {
896
- // ...
897
-
898
- $builder->add('task', 'task');
899
- // Note that the property ``task`` (first argument)
900
- // is defined as a ``task`` form type (second).
901
- }
902
-
903
- // ...
904
- }
905
-
906
- Read :ref: `form-cookbook-form-field-service ` for more information.
907
-
908
836
Placing the form logic into its own class means that the form can be easily
909
837
reused elsewhere in your project. This is the best way to create forms, but
910
838
the choice is ultimately up to you.
@@ -955,6 +883,72 @@ the choice is ultimately up to you.
955
883
956
884
$form->get('dueDate')->getData();
957
885
886
+ Defining your Forms as Services
887
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
888
+
889
+ Defining your form type as a service is a good practice and makes it really
890
+ easy to use in your application.
891
+
892
+ .. configuration-block ::
893
+
894
+ .. code-block :: yaml
895
+
896
+ # src/Acme/TaskBundle/Resources/config/services.yml
897
+ services :
898
+ acme_demo.form.type.task :
899
+ class : Acme\TaskBundle\Form\Type\TaskType
900
+ tags :
901
+ - { name: form.type, alias: task }
902
+
903
+ .. code-block :: xml
904
+
905
+ <!-- src/Acme/TaskBundle/Resources/config/services.xml -->
906
+ <service id =" acme_demo.form.type.task" class =" Acme\TaskBundle\Form\Type\TaskType" >
907
+ <tag name =" form.type" alias =" task" />
908
+ </service >
909
+
910
+ .. code-block :: php
911
+
912
+ // src/Acme/TaskBundle/Resources/config/services.php
913
+ use Symfony\Component\DependencyInjection\Definition;
914
+
915
+ $container
916
+ ->register('acme_demo.form.type.task', 'Acme\TaskBundle\Form\Type\TaskType')
917
+ ->addTag('form.type', array(
918
+ 'alias' => 'task',
919
+ ))
920
+ ;
921
+
922
+ That's it! Now you can use your form type directly in a controller::
923
+
924
+ // src/Acme/TaskBundle/Controller/DefaultController.php
925
+ // ...
926
+
927
+ public function newAction()
928
+ {
929
+ $task = ...;
930
+ $form = $this->createForm('task', $task);
931
+
932
+ // ...
933
+ }
934
+
935
+ or even use from within the form type of another form::
936
+
937
+ // src/Acme/TaskBundle/Form/Type/ListType.php
938
+ // ...
939
+
940
+ class ListType extends AbstractType
941
+ {
942
+ public function buildForm(FormBuilderInterface $builder, array $options)
943
+ {
944
+ // ...
945
+
946
+ $builder->add('someTask', 'task');
947
+ }
948
+ }
949
+
950
+ Read :ref: `form-cookbook-form-field-service ` for more information.
951
+
958
952
.. index ::
959
953
pair: Forms; Doctrine
960
954
0 commit comments