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

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