diff --git a/README.md b/README.md index f0d1334..031aa4a 100644 --- a/README.md +++ b/README.md @@ -38,8 +38,9 @@ class PostType extends AbstractType { $builder // ... - ->add('attachments', CollectionJsType::class, [ - 'entry_type' => FileType::class, + ->add('authors', CollectionJsType::class, [ + 'entry_type' => TextType::class, + 'prototype' => true, 'allow_add' => true, 'allow_delete' => true, 'allow_move_up' => true, diff --git a/src/Form/CollectionJsType.php b/src/Form/CollectionJsType.php index abfb742..40bcd61 100644 --- a/src/Form/CollectionJsType.php +++ b/src/Form/CollectionJsType.php @@ -3,7 +3,9 @@ namespace Tienvx\UX\CollectionJs\Form; use Symfony\Component\Form\AbstractType; +use Symfony\Component\Form\Exception\InvalidConfigurationException; use Symfony\Component\Form\Extension\Core\Type\CollectionType; +use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\FormView; use Symfony\Component\OptionsResolver\Options; @@ -19,6 +21,28 @@ public function getParent(): string return CollectionType::class; } + /** + * {@inheritdoc} + */ + public function buildForm(FormBuilderInterface $builder, array $options) + { + if (!$options['prototype']) { + throw new InvalidConfigurationException(sprintf('You must enable prototype for form type %s.', self::class)); + } + + $prototypeOptions = array_replace([ + 'required' => $options['required'], + 'label' => $options['prototype_name'] . 'label__', + ], $options['entry_options']); + + if (null !== $options['prototype_data']) { + $prototypeOptions['data'] = $options['prototype_data']; + } + + $prototype = $builder->create($options['prototype_name'], $options['entry_type'], $prototypeOptions); + $builder->setAttribute('prototype', $prototype->getForm()); + } + /** * {@inheritdoc} */ diff --git a/src/Resources/views/bootstrap_3_layout.html.twig b/src/Resources/views/bootstrap_3_layout.html.twig index 412a155..79a6b2f 100644 --- a/src/Resources/views/bootstrap_3_layout.html.twig +++ b/src/Resources/views/bootstrap_3_layout.html.twig @@ -1,12 +1,6 @@ {% use "@CollectionJs/bootstrap_base_layout.html.twig" %} -{% block collection_js_accordion %} -