@@ -7,7 +7,7 @@ How to Create a Custom Form Field Type
77Symfony comes with a bunch of core field types available for building forms.
88However there are situations where you may want to create a custom form field
99type for a specific purpose. This recipe assumes you need a field definition
10- that holds aperson's gender , based on the existing choice field. This section
10+ that holds ashipping option , based on the existing choice field. This section
1111explains how the field is defined, how you can customize its layout and finally,
1212how you can register it for use in your application.
1313
@@ -16,25 +16,26 @@ Defining the Field Type
1616
1717In order to create the custom field type, first you have to create the class
1818representing the field. In this situation the class holding the field type
19- will be called ``GenderType `` and the file will be stored in the default location
19+ will be called ``ShippingType `` and the file will be stored in the default location
2020for form fields, which is ``<BundleName>\Form\Type ``. Make sure the field extends
2121:class: `Symfony\\ Component\\ Form\\ AbstractType `::
2222
23- // src/AppBundle/Form/Type/GenderType .php
23+ // src/AppBundle/Form/Type/ShippingType .php
2424 namespace AppBundle\Form\Type;
2525
2626 use Symfony\Component\Form\AbstractType;
2727 use Symfony\Component\OptionsResolver\OptionsResolver;
2828 use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
2929
30- classGenderType extends AbstractType
30+ classShippingType extends AbstractType
3131 {
3232 public function configureOptions(OptionsResolver $resolver)
3333 {
3434 $resolver->setDefaults(array(
3535 'choices' => array(
36- 'm' => 'Male',
37- 'f' => 'Female',
36+ 'Standard Shipping' => 'standard',
37+ 'Expedited Shipping' => 'expedited',
38+ 'Priority Shipping' => 'priority',
3839 )
3940 ));
4041 }
@@ -67,8 +68,8 @@ important:
6768 This method is used to set any extra variables you'll
6869 need when rendering your field in a template. For example, in `ChoiceType `_,
6970 a ``multiple `` variable is set and used in the template to set (or not
70- set) the ``multiple `` attribute on the ``select `` field. See ` Creating a Template for the Field `_
71- for more details.
71+ set) the ``multiple `` attribute on the ``select `` field. See
72+ ` Creating a Template for the Field `_ for more details.
7273
7374``configureOptions() ``
7475 This defines options for your form type that
@@ -83,9 +84,9 @@ important:
8384 Also, if you need to modify the "view" of any of your child types from
8485 your parent type, use the ``finishView() `` method.
8586
86- The goal of this field was to extend the choice type to enable selection of
87- a gender . This is achieved by fixing the ``choices `` to a list ofpossible
88- genders .
87+ The goal of this field was to extend the choice type to enable selection of the
88+ shipping type . This is achieved by fixing the ``choices `` to a list ofavailable
89+ shipping options .
8990
9091Creating a Template for the Field
9192---------------------------------
@@ -95,9 +96,9 @@ the class name of your type. For more information, see :ref:`form-customization-
9596
9697..note ::
9798
98- The first part of the prefix (e.g. ``gender ``) comes from the class name
99- (``GenderType `` -> ``gender ``). This can be controlled by overriding ``getBlockPrefix() ``
100- in ``GenderType ``.
99+ The first part of the prefix (e.g. ``shipping ``) comes from the class name
100+ (``ShippingType `` -> ``shipping ``). This can be controlled by overriding ``getBlockPrefix() ``
101+ in ``ShippingType ``.
101102
102103..caution ::
103104
@@ -113,14 +114,14 @@ any work as the custom field type will automatically be rendered like a ``Choice
113114But for the sake of this example, suppose that when your field is "expanded"
114115(i.e. radio buttons or checkboxes, instead of a select field), you want to
115116always render it in a ``ul `` element. In your form theme template (see above
116- link for details), create a ``gender_widget `` block to handle this:
117+ link for details), create a ``shipping_widget `` block to handle this:
117118
118119..configuration-block ::
119120
120121 ..code-block ::html+twig
121122
122123 {# app/Resources/views/form/fields.html.twig #}
123- {% blockgender_widget %}
124+ {% blockshipping_widget %}
124125 {% spaceless %}
125126 {% if expanded %}
126127 <ul {{ block('widget_container_attributes') }}>
@@ -140,7 +141,7 @@ link for details), create a ``gender_widget`` block to handle this:
140141
141142 ..code-block ::html+php
142143
143- <!-- app/Resources/views/form/gender_widget .html.php -->
144+ <!-- app/Resources/views/form/shipping_widget .html.php -->
144145 <?php if ($expanded) : ?>
145146 <ul <?php $view['form']->block($form, 'widget_container_attributes') ?>>
146147 <?php foreach ($form as $child) : ?>
@@ -158,7 +159,7 @@ link for details), create a ``gender_widget`` block to handle this:
158159..note ::
159160
160161 Make sure the correct widget prefix is used. In this example the name should
161- be ``gender_widget `` (see:ref: `form-customization-form-themes `).
162+ be ``shipping_widget `` (see:ref: `form-customization-form-themes `).
162163 Further, the main config file should point to the custom form template
163164 so that it's used when rendering all forms.
164165
@@ -255,20 +256,20 @@ new instance of the type in one of your forms::
255256
256257 use Symfony\Component\Form\AbstractType;
257258 use Symfony\Component\Form\FormBuilderInterface;
258- use AppBundle\Form\Type\GenderType ;
259+ use AppBundle\Form\Type\ShippingType ;
259260
260- classAuthorType extends AbstractType
261+ classOrderType extends AbstractType
261262 {
262263 public function buildForm(FormBuilderInterface $builder, array $options)
263264 {
264- $builder->add('gender_code ',GenderType ::class, array(
265- 'placeholder' => 'Choose agender ',
265+ $builder->add('shipping_code ',ShippingType ::class, array(
266+ 'placeholder' => 'Choose adelivery option ',
266267 ));
267268 }
268269 }
269270
270- But this only works because the ``GenderType `` is very simple. What if
271- thegender codes were stored in configuration or in a database? The next
271+ But this only works because the ``ShippingType() `` is very simple. What if
272+ theshipping codes were stored in configuration or in a database? The next
272273section explains how more complex field types solve this problem.
273274
274275.. _form-field-service :
@@ -280,13 +281,13 @@ Accessing Services and Config
280281If you need to access:doc: `services </service_container >` from your form class,
281282add a ``__construct() `` method like normal::
282283
283- // src/AppBundle/Form/Type/GenderType .php
284+ // src/AppBundle/Form/Type/ShippingType .php
284285 namespace AppBundle\Form\Type;
285286
286287 // ...
287288 use Doctrine\ORM\EntityManagerInterface;
288289
289- classGenderType extends AbstractType
290+ classShippingType extends AbstractType
290291 {
291292 private $em;
292293