From a7ac57fb02fd5cfc0c1e6c0b37353675a20032d1 Mon Sep 17 00:00:00 2001 From: WouterJ Date: Fri, 8 Feb 2013 18:57:12 +0100 Subject: [PATCH] Added missing formats --- cookbook/form/create_custom_field_type.rst | 116 ++++++++++++++----- cookbook/form/create_form_type_extension.rst | 19 +-- cookbook/form/data_transformers.rst | 14 ++- cookbook/form/dynamic_form_modification.rst | 3 +- cookbook/form/form_collections.rst | 20 +++- cookbook/form/form_customization.rst | 4 + 6 files changed, 139 insertions(+), 37 deletions(-) diff --git a/cookbook/form/create_custom_field_type.rst b/cookbook/form/create_custom_field_type.rst index 9d094ed72c7..88f59d0547b 100644 --- a/cookbook/form/create_custom_field_type.rst +++ b/cookbook/form/create_custom_field_type.rst @@ -104,26 +104,45 @@ 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: -.. code-block:: html+jinja - - {# src/Acme/DemoBundle/Resources/views/Form/fields.html.twig #} - {% block gender_widget %} - {% spaceless %} - {% if expanded %} - - {% else %} - {# just let the choice widget render the select tag #} - {{ block('choice_widget') }} - {% endif %} - {% endspaceless %} - {% endblock %} +.. configuration-block:: + + .. code-block:: html+jinja + + {# src/Acme/DemoBundle/Resources/views/Form/fields.html.twig #} + {% block gender_widget %} + {% spaceless %} + {% if expanded %} + + {% else %} + {# just let the choice widget render the select tag #} + {{ block('choice_widget') }} + {% endif %} + {% endspaceless %} + {% endblock %} + + .. code-block:: html+php + + + + + + + renderBlock('choice_widget') ?> + .. note:: @@ -132,13 +151,35 @@ you want to always render it in a ``ul`` element. In your form theme template Further, the main config file should point to the custom form template so that it's used when rendering all forms. - .. code-block:: yaml + .. configuration-block:: - # app/config/config.yml - twig: - form: - resources: - - 'AcmeDemoBundle:Form:fields.html.twig' + .. code-block:: yaml + + # app/config/config.yml + twig: + form: + resources: + - 'AcmeDemoBundle:Form:fields.html.twig' + + .. code-block:: xml + + + + + AcmeDemoBundle:Form:fields.html.twig + + + + .. code-block:: php + + // app/config/config.php + $container->loadFromExtension('twig', array( + 'form' => array( + 'resources' => array( + 'AcmeDemoBundle:Form:fields.html.twig', + ), + ), + )); Using the Field Type -------------------- @@ -194,6 +235,12 @@ example, suppose that you're storing the gender parameters in configuration: + .. code-block:: php + + // app/config/config.php + $container->setParameter('genders.m', 'Male'); + $container->setParameter('genders.f', 'Female'); + To use the parameter, define your custom field type as a service, injecting the ``genders`` parameter value as the first argument to its to-be-created ``__construct`` function: @@ -219,6 +266,21 @@ the ``genders`` parameter value as the first argument to its to-be-created + .. code-block:: php + + // src/Acme/DemoBundle/Resources/config/services.php + use Symfony\Component\DependencyInjection\Definition; + + $container + ->setDefinition('acme_demo.form.type.gender', new Definition( + 'Acme\DemoBundle\Form\Type\GenderType', + array('%genders%') + )) + ->addTag('form.type', array( + 'alias' => 'gender', + )) + ; + .. tip:: Make sure the services file is being imported. See :ref:`service-container-imports-directive` @@ -231,8 +293,8 @@ argument to ``GenderType``, which receives the gender configuration:: // src/Acme/DemoBundle/Form/Type/GenderType.php namespace Acme\DemoBundle\Form\Type; - // ... + // ... class GenderType extends AbstractType { private $genderChoices; diff --git a/cookbook/form/create_form_type_extension.rst b/cookbook/form/create_form_type_extension.rst index 4444b5053f7..95983a33b69 100644 --- a/cookbook/form/create_form_type_extension.rst +++ b/cookbook/form/create_form_type_extension.rst @@ -112,14 +112,19 @@ tag: .. code-block:: xml - + .. code-block:: php $container - ->register('acme_demo_bundle.image_type_extension', 'Acme\DemoBundle\Form\Extension\ImageTypeExtension') + ->register( + 'acme_demo_bundle.image_type_extension', + 'Acme\DemoBundle\Form\Extension\ImageTypeExtension' + ) ->addTag('form.type_extension', array('alias' => 'file')); The ``alias`` key of the tag is the type of field that this extension should @@ -219,8 +224,8 @@ it in the view:: /** * Store the image_path option as a builder attribute * - * @param \Symfony\Component\Form\FormBuilder $builder - * @param array $options + * @param FormBuilder $builder + * @param array $options */ public function buildForm(FormBuilder $builder, array $options) { @@ -232,8 +237,8 @@ it in the view:: /** * Pass the image url to the view * - * @param \Symfony\Component\Form\FormView $view - * @param \Symfony\Component\Form\FormInterface $form + * @param FormView $view + * @param FormInterface $form */ public function buildView(FormView $view, FormInterface $form) { @@ -326,4 +331,4 @@ next to the file field. For example:: } When displaying the form, if the underlying model has already been associated -with an image, you will see it displayed next to the file input. \ No newline at end of file +with an image, you will see it displayed next to the file input. diff --git a/cookbook/form/data_transformers.rst b/cookbook/form/data_transformers.rst index 802497a79b4..53927f75feb 100644 --- a/cookbook/form/data_transformers.rst +++ b/cookbook/form/data_transformers.rst @@ -65,7 +65,9 @@ for converting to and from the issue number and the Issue object:: * Transforms a string (number) to an object (issue). * * @param string $number + * * @return Issue|null + * * @throws TransformationFailedException if object (issue) is not found. */ public function reverseTransform($number) @@ -287,6 +289,17 @@ it's recognized as a custom field type: + .. code-block:: php + + $container + ->setDefinition('acme_demo.type.issue_selector', array( + '@doctrine.orm.entity_manager' + )) + ->addTag('form.type', array( + 'alias' => 'issue_selector', + )) + ; + Now, whenever you need to use your special ``issue_selector`` field type, it's quite easy:: @@ -311,4 +324,3 @@ it's quite easy:: return 'task'; } } - diff --git a/cookbook/form/dynamic_form_modification.rst b/cookbook/form/dynamic_form_modification.rst index 7116cf6823c..4e676bb717c 100644 --- a/cookbook/form/dynamic_form_modification.rst +++ b/cookbook/form/dynamic_form_modification.rst @@ -39,7 +39,8 @@ from this class will look the exact same regardless if a new Product is being cr or if an existing product is being edited (e.g. a product fetched from the database). Suppose now, that you don't want the user to be able to change the ``name`` value -once the object has been created. To do this, you can rely on Symfony's :doc:`Event Dispatcher ` +once the object has been created. To do this, you can rely on Symfony's +:doc:`Event Dispatcher ` system to analyze the data on the object and modify the form based on the Product object's data. In this entry, you'll learn how to add this level of flexibility to your forms. diff --git a/cookbook/form/form_collections.rst b/cookbook/form/form_collections.rst index f54e0d61ef5..9c1ba931f0e 100755 --- a/cookbook/form/form_collections.rst +++ b/cookbook/form/form_collections.rst @@ -179,7 +179,7 @@ In your controller, you'll now initialize a new instance of ``TaskType``:: if ('POST' === $request->getMethod()) { $form->bindRequest($request); if ($form->isValid()) { - // maybe do some form processing, like saving the Task and Tag objects + // ... maybe do some form processing, like saving the Task and Tag objects } } @@ -459,6 +459,24 @@ into new ``Tag`` objects and added to the ``tags`` property of the ``Task`` obje targetEntity: Tag cascade: [persist] + .. code-block:: xml + + + + + + + + + + + + + + A second potential issue deals with the `Owning Side and Inverse Side`_ of Doctrine relationships. In this example, if the "owning" side of the relationship is "Task", then persistence will work fine as the tags are diff --git a/cookbook/form/form_customization.rst b/cookbook/form/form_customization.rst index 1bb3f1598e7..d6c874655d4 100644 --- a/cookbook/form/form_customization.rst +++ b/cookbook/form/form_customization.rst @@ -729,6 +729,7 @@ and customize the ``field_errors`` fragment. .. tip:: + See :ref:`cookbook-form-theming-methods` for how to apply this customization. You can also customize the error output for just one specific field type. @@ -782,6 +783,7 @@ class to the ``div`` element around each row: .. tip:: + See :ref:`cookbook-form-theming-methods` for how to apply this customization. Adding a "Required" Asterisk to Field Labels @@ -836,6 +838,7 @@ original template: .. tip:: + See :ref:`cookbook-form-theming-methods` for how to apply this customization. Adding "help" messages @@ -905,6 +908,7 @@ To render a help message below a field, pass in a ``help`` variable: widget($form['title'], array('help' => 'foobar')) ?> .. tip:: + See :ref:`cookbook-form-theming-methods` for how to apply this customization. Using Form Variables