Skip to content

Instantly share code, notes, and snippets.

@Venzon
Last active August 29, 2015 13:55
Show Gist options
  • Save Venzon/8694089 to your computer and use it in GitHub Desktop.
Save Venzon/8694089 to your computer and use it in GitHub Desktop.
Symfony2 Collection Type with default value support by prototype_data option
<?php
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
class CollectionPrototypeDataType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
parent::buildForm($builder, $options);
if (null !== ($prototypeFormBuilder = $builder->getAttribute('prototype'))) {
/** @var $prototypeFormBuilder FormBuilderInterface */
$prototypeFormBuilder->setData($options['prototype_data']);
}
}
/**
* {@inheritdoc}
*/
public function getName()
{
return 'collection';
}
/**
* {@inheritdoc}
*/
public function getParent()
{
return 'collection';
}
}
@leevigraham
Copy link

I think extending collection with a type with the name collection causes an infinite loop in 2.5.

@jumika
Copy link

jumika commented May 27, 2014

I've forked this gist and converted it to a form type extension.
see: https://gist.github.com/jumika/e2f0a5b3d4faf277307a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment