diff --git a/components/config/definition.rst b/components/config/definition.rst index d82df6ac634..559ed61c0dd 100644 --- a/components/config/definition.rst +++ b/components/config/definition.rst @@ -141,13 +141,14 @@ values:: $rootNode ->children() - ->enumNode('gender') - ->values(array('male', 'female')) + ->enumNode('delivery') + ->values(array('standard', 'expedited', 'priority')) ->end() ->end() ; -This will restrict the ``gender`` option to be either ``male`` or ``female``. +This will restrict the ``delivery`` options to be either ``standard``, +``expedited`` or ``priority``. Array Nodes ~~~~~~~~~~~ diff --git a/form/create_custom_field_type.rst b/form/create_custom_field_type.rst index fc8c0d905f2..b1c50bf7b4e 100644 --- a/form/create_custom_field_type.rst +++ b/form/create_custom_field_type.rst @@ -7,7 +7,7 @@ How to Create a Custom Form Field Type Symfony comes with a bunch of core field types available for building forms. However there are situations where you may want to create a custom form field type for a specific purpose. This recipe assumes you need a field definition -that holds a person's gender, based on the existing choice field. This section +that holds a shipping option, based on the existing choice field. This section explains how the field is defined, how you can customize its layout and finally, how you can register it for use in your application. @@ -16,24 +16,25 @@ Defining the Field Type In order to create the custom field type, first you have to create the class representing the field. In this situation the class holding the field type -will be called ``GenderType`` and the file will be stored in the default location +will be called ``ShippingType`` and the file will be stored in the default location for form fields, which is ``\Form\Type``. Make sure the field extends :class:`Symfony\\Component\\Form\\AbstractType`:: - // src/AppBundle/Form/Type/GenderType.php + // src/AppBundle/Form/Type/ShippingType.php namespace AppBundle\Form\Type; use Symfony\Component\Form\AbstractType; use Symfony\Component\OptionsResolver\OptionsResolver; - class GenderType extends AbstractType + class ShippingType extends AbstractType { public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults(array( 'choices' => array( - 'm' => 'Male', - 'f' => 'Female', + 'standard' => 'Standard Shipping', + 'expedited' => 'Expedited Shipping', + 'priority' => 'Priority Shipping', ) )); } @@ -45,7 +46,7 @@ for form fields, which is ``\Form\Type``. Make sure the field extend public function getName() { - return 'app_gender'; + return 'app_shipping'; } } @@ -69,8 +70,8 @@ important: This method is used to set any extra variables you'll need when rendering your field in a template. For example, in `ChoiceType`_, a ``multiple`` variable is set and used in the template to set (or not - set) the ``multiple`` attribute on the ``select`` field. See `Creating a Template for the Field`_ - for more details. + set) the ``multiple`` attribute on the ``select`` field. See + `Creating a Template for the Field`_ for more details. .. versionadded:: 2.7 The ``configureOptions()`` method was introduced in Symfony 2.7. Previously, @@ -93,9 +94,9 @@ The ``getName()`` method returns an identifier which should be unique in your application. This is used in various places, such as when customizing how your form type will be rendered. -The goal of this field was to extend the choice type to enable selection of -a gender. This is achieved by fixing the ``choices`` to a list of possible -genders. +The goal of this field was to extend the choice type to enable selection of the +shipping type. This is achieved by fixing the ``choices`` to a list of available +shipping options. Creating a Template for the Field --------------------------------- @@ -109,14 +110,14 @@ any work as the custom field type will automatically be rendered like a ``choice type. But for the sake of this example, suppose that when your field is "expanded" (i.e. radio buttons or checkboxes, instead of a select field), you want to always render it in a ``ul`` element. In your form theme template (see above -link for details), create a ``gender_widget`` block to handle this: +link for details), create a ``shipping_widget`` block to handle this: .. configuration-block:: .. code-block:: html+twig {# app/Resources/views/form/fields.html.twig #} - {% block gender_widget %} + {% block shipping_widget %} {% spaceless %} {% if expanded %}