From 2950097cf1337d03b8a6dc2b80b2beb5892b3a28 Mon Sep 17 00:00:00 2001 From: WouterJ Date: Sat, 30 Mar 2013 22:53:15 +0100 Subject: [PATCH] Refactored docs to use adders/removers --- cookbook/form/form_collections.rst | 130 +++++++++++++++++++---------- 1 file changed, 87 insertions(+), 43 deletions(-) diff --git a/cookbook/form/form_collections.rst b/cookbook/form/form_collections.rst index be64bb969b7..cb5c392700c 100755 --- a/cookbook/form/form_collections.rst +++ b/cookbook/form/form_collections.rst @@ -53,11 +53,6 @@ objects. Start by creating a simple ``Task`` class:: { return $this->tags; } - - public function setTags(ArrayCollection $tags) - { - $this->tags = $tags; - } } .. note:: @@ -240,9 +235,9 @@ zero tags when first created). -When the user submits the form, the submitted data for the ``Tags`` fields -are used to construct an ArrayCollection of ``Tag`` objects, which is then -set on the ``tag`` field of the ``Task`` instance. +When the user submits the form, the submitted data for the ``Tags`` fields are +used to construct an ``ArrayCollection`` of ``Tag`` objects, which is then set +on the ``tag`` field of the ``Task`` instance. The ``Tags`` collection is accessible naturally via ``$task->getTags()`` and can be persisted to the database or used however you need. @@ -286,7 +281,6 @@ add the ``allow_add`` option to your collection field:: // src/Acme/TaskBundle/Form/Type/TaskType.php // ... - use Symfony\Component\Form\FormBuilderInterface; public function buildForm(FormBuilderInterface $builder, array $options) @@ -296,18 +290,59 @@ add the ``allow_add`` option to your collection field:: $builder->add('tags', 'collection', array( 'type' => new TagType(), 'allow_add' => true, - 'by_reference' => false, )); } -Note that ``'by_reference' => false`` was also added. Normally, the form -framework would modify the tags on a `Task` object *without* actually -ever calling `setTags`. By setting :ref:`by_reference` -to `false`, `setTags` will be called. This will be important later as you'll -see. +Now that the form type knows tags can be added, the ``Tasks`` class needs to +add methods to make editing the tags possible. This is done by creating an +"adder". In this case, the adder will be ``addTag``:: + + // src/Acme/TaskBundle/Entity/Task.php + namespace Acme\TaskBundle\Entity; + + // ... + class Task + { + // ... + + public function addTag($tag) + { + $this->tags->add($tag); + } + + public function removeTag($tag) + { + // ... + } + } + +But, the form type will still use ``getTags`` now. You need to set the +``by_reference`` option to ``false``, otherwise the form type will get the +object by using the getter and change that object. + +.. caution:: + + If no ``addTag`` **and** ``removeTag`` method is found, the form will + still ``setTag`` when setting ``by_reference`` to ``false``. You'll learn + more about the ``removeTag`` method later in this article. + +.. code-block:: php + + // src/Acme/TaskBundle/Form/Type/TaskType.php + + // ... + public function buildForm(FormBuilderInterface $builder, array $options) + { + // ... + + $builder->add('tags', 'collection', array( + // ... + 'by_reference' => false, + )); + } In addition to telling the field to accept any number of submitted objects, the -``allow_add`` also makes a "prototype" variable available to you. This "prototype" +``allow_add`` also makes a *"prototype"* variable available to you. This "prototype" is a little "template" that contains all the HTML to be able to render any new "tag" forms. To render it, make the following change to your template: @@ -321,7 +356,9 @@ new "tag" forms. To render it, make the following change to your template: .. code-block:: html+php -