symfony/symfony-docs — Issue #1057: [Form] Document usage of adders and removers
Author: bschussek
Created: 2012-02-02 11:14:26+00:00
State: open

As of symfony/symfony#3239 the "collection" and "choice" types (with multiple=true) as well as subtypes (such as "entity") will try to discover an adder and a remover method in your model.

Example form:

$form = $this->createFormBuilder($article)
->add('tags')
->getForm();

The underlying article class is expected to look like this:

class Article
{
public function addTag($tag) { ... }
public function removeTag($tag) { ... }
public function getTags($tag) { ... }
}

If adder and remover are not found, the form behaves differently depending on whether `getTags` returns an object or an array.

1. If it returns an object (collection), the object is modified by reference and not written back
2. If it returns an array, the array is modified and written back using `setTags`

You can force a write-back in case (1) by setting the field option "by_reference" to false. Note that in this case, adders and removers **won't be used** even if present! Instead, the expected object signature is

class Article
{
public function setTags($tags) { ... }
public function getTags($tag) { ... }
}