vendor/symfony/form/Extension/Core/Type/RepeatedType.php line 19

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\DataTransformer\ValueToDuplicatesTransformer;
  13. use Symfony\Component\Form\FormBuilderInterface;
  14. use Symfony\Component\OptionsResolver\OptionsResolver;
  15. class RepeatedType extends AbstractType
  16. {
  17.     /**
  18.      * {@inheritdoc}
  19.      */
  20.     public function buildForm(FormBuilderInterface $builder, array $options)
  21.     {
  22.         // Overwrite required option for child fields
  23.         $options['first_options']['required'] = $options['required'];
  24.         $options['second_options']['required'] = $options['required'];
  25.         if (!isset($options['options']['error_bubbling'])) {
  26.             $options['options']['error_bubbling'] = $options['error_bubbling'];
  27.         }
  28.         // children fields must always be mapped
  29.         $defaultOptions = ['mapped' => true];
  30.         $builder
  31.             ->addViewTransformer(new ValueToDuplicatesTransformer([
  32.                 $options['first_name'],
  33.                 $options['second_name'],
  34.             ]))
  35.             ->add($options['first_name'], $options['type'], array_merge($options['options'], $options['first_options'], $defaultOptions))
  36.             ->add($options['second_name'], $options['type'], array_merge($options['options'], $options['second_options'], $defaultOptions))
  37.         ;
  38.     }
  39.     /**
  40.      * {@inheritdoc}
  41.      */
  42.     public function configureOptions(OptionsResolver $resolver)
  43.     {
  44.         $resolver->setDefaults([
  45.             'type' => TextType::class,
  46.             'options' => [],
  47.             'first_options' => [],
  48.             'second_options' => [],
  49.             'first_name' => 'first',
  50.             'second_name' => 'second',
  51.             'error_bubbling' => false,
  52.             'invalid_message' => 'The values do not match.',
  53.         ]);
  54.         $resolver->setAllowedTypes('options''array');
  55.         $resolver->setAllowedTypes('first_options''array');
  56.         $resolver->setAllowedTypes('second_options''array');
  57.     }
  58.     /**
  59.      * {@inheritdoc}
  60.      */
  61.     public function getBlockPrefix(): string
  62.     {
  63.         return 'repeated';
  64.     }
  65. }