This is tricky since you aren't just embedding a collection of Tag forms that are say, all separate text fields. I suppose you could do that with some trickery, but what about using a data transformer instead? You could convert a comma-separated list of tags to an ArrayCollection and pass that back to the form, and on the flip-side, take the collection and return the tags as a comma-separated string.
Data transformer
FiddleTagsTransformer.php
<?php
namespace Fuz\AppBundle\Transformer;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Form\DataTransformerInterface;
use Fuz\AppBundle\Entity\FiddleTag;
class FiddleTagTransformer implements DataTransformerInterface
{
public function transform($tagCollection)
{
$tags = array();
foreach ($tagCollection as $fiddleTag)
{
$tags[] = $fiddleTag->getTag();
}
return implode(',', $tags);
}
public function reverseTransform($tags)
{
$tagCollection = new ArrayCollection();
foreach (explode(',', $tags) as $tag)
{
$fiddleTag = new FiddleTag();
$fiddleTag->setTag($tag);
$tagCollection->add($fiddleTag);
}
return $tagCollection;
}
}
Note: you cannot specify ArrayCollection type to public function transform($tagCollection)
because your implementation should match the interface.
Form type
The second step is to replace your form field declaration so it will use the data transformer transparently, you'll not even need to do anything in your controller:
FiddleType.php
$builder
// ...
->add(
$builder
->create('tags', 'text', array(
'required' => false,
))
->addModelTransformer(new FiddleTagTransformer())
)
;
Validation
You can use @Assert\Count to limit the number of allowed tags, and @Assert\Valid if your FiddleTag entity has some validation constraints itself.
Fiddle.php
/**
* @var ArrayCollection[FiddleTag]
*
* @ORM\OneToMany(targetEntity="FiddleTag", mappedBy="fiddle", cascade={"all"}, orphanRemoval=true)
* @Assert\Count(max = 5, maxMessage = "You can't set more than 5 tags.")
* @Assert\Valid()
*/
protected $tags;
Further reading
See the Symfony2 doc about data transformers: http://symfony.com/doc/current/cookbook/form/data_transformers.html
See these posts for some other ideas:
Parsing comma separated string into multiple database entries (eg. Tags)
How does Symfony 2 find custom form types?