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

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