vendor/symfony/form/Extension/Core/Type/CollectionType.php line 22

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Form\Extension\Core\Type;
  11. use Symfony\Component\Form\AbstractType;
  12. use Symfony\Component\Form\Extension\Core\EventListener\ResizeFormListener;
  13. use Symfony\Component\Form\FormBuilderInterface;
  14. use Symfony\Component\Form\FormInterface;
  15. use Symfony\Component\Form\FormView;
  16. use Symfony\Component\OptionsResolver\Options;
  17. use Symfony\Component\OptionsResolver\OptionsResolver;
  18. class CollectionType extends AbstractType
  19. {
  20.     /**
  21.      * {@inheritdoc}
  22.      */
  23.     public function buildForm(FormBuilderInterface $builder, array $options)
  24.     {
  25.         if ($options['allow_add'] && $options['prototype']) {
  26.             $prototypeOptions array_replace([
  27.                 'required' => $options['required'],
  28.                 'label' => $options['prototype_name'].'label__',
  29.             ], $options['entry_options']);
  30.             if (null !== $options['prototype_data']) {
  31.                 $prototypeOptions['data'] = $options['prototype_data'];
  32.             }
  33.             $prototype $builder->create($options['prototype_name'], $options['entry_type'], $prototypeOptions);
  34.             $builder->setAttribute('prototype'$prototype->getForm());
  35.         }
  36.         $resizeListener = new ResizeFormListener(
  37.             $options['entry_type'],
  38.             $options['entry_options'],
  39.             $options['allow_add'],
  40.             $options['allow_delete'],
  41.             $options['delete_empty']
  42.         );
  43.         $builder->addEventSubscriber($resizeListener);
  44.     }
  45.     /**
  46.      * {@inheritdoc}
  47.      */
  48.     public function buildView(FormView $viewFormInterface $form, array $options)
  49.     {
  50.         $view->vars array_replace($view->vars, [
  51.             'allow_add' => $options['allow_add'],
  52.             'allow_delete' => $options['allow_delete'],
  53.         ]);
  54.         if ($form->getConfig()->hasAttribute('prototype')) {
  55.             $prototype $form->getConfig()->getAttribute('prototype');
  56.             $view->vars['prototype'] = $prototype->setParent($form)->createView($view);
  57.         }
  58.     }
  59.     /**
  60.      * {@inheritdoc}
  61.      */
  62.     public function finishView(FormView $viewFormInterface $form, array $options)
  63.     {
  64.         if ($form->getConfig()->hasAttribute('prototype') && $view->vars['prototype']->vars['multipart']) {
  65.             $view->vars['multipart'] = true;
  66.         }
  67.     }
  68.     /**
  69.      * {@inheritdoc}
  70.      */
  71.     public function configureOptions(OptionsResolver $resolver)
  72.     {
  73.         $entryOptionsNormalizer = function (Options $options$value) {
  74.             $value['block_name'] = 'entry';
  75.             return $value;
  76.         };
  77.         $resolver->setDefaults([
  78.             'allow_add' => false,
  79.             'allow_delete' => false,
  80.             'prototype' => true,
  81.             'prototype_data' => null,
  82.             'prototype_name' => '__name__',
  83.             'entry_type' => TextType::class,
  84.             'entry_options' => [],
  85.             'delete_empty' => false,
  86.         ]);
  87.         $resolver->setNormalizer('entry_options'$entryOptionsNormalizer);
  88.         $resolver->setAllowedTypes('delete_empty', ['bool''callable']);
  89.     }
  90.     /**
  91.      * {@inheritdoc}
  92.      */
  93.     public function getBlockPrefix()
  94.     {
  95.         return 'collection';
  96.     }
  97. }