vendor/symfony/form/Extension/Validator/Type/FormTypeValidatorExtension.php line 34

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\Validator\Type;
  11. use Symfony\Component\Form\Extension\Core\Type\FormType;
  12. use Symfony\Component\Form\Extension\Validator\EventListener\ValidationListener;
  13. use Symfony\Component\Form\Extension\Validator\ViolationMapper\ViolationMapper;
  14. use Symfony\Component\Form\FormBuilderInterface;
  15. use Symfony\Component\Form\FormRendererInterface;
  16. use Symfony\Component\OptionsResolver\Options;
  17. use Symfony\Component\OptionsResolver\OptionsResolver;
  18. use Symfony\Component\Validator\Constraint;
  19. use Symfony\Component\Validator\Validator\ValidatorInterface;
  20. use Symfony\Contracts\Translation\TranslatorInterface;
  21. /**
  22.  * @author Bernhard Schussek <bschussek@gmail.com>
  23.  */
  24. class FormTypeValidatorExtension extends BaseValidatorExtension
  25. {
  26.     private ValidatorInterface $validator;
  27.     private ViolationMapper $violationMapper;
  28.     private bool $legacyErrorMessages;
  29.     public function __construct(ValidatorInterface $validatorbool $legacyErrorMessages trueFormRendererInterface $formRenderer nullTranslatorInterface $translator null)
  30.     {
  31.         $this->validator $validator;
  32.         $this->violationMapper = new ViolationMapper($formRenderer$translator);
  33.     }
  34.     /**
  35.      * {@inheritdoc}
  36.      */
  37.     public function buildForm(FormBuilderInterface $builder, array $options)
  38.     {
  39.         $builder->addEventSubscriber(new ValidationListener($this->validator$this->violationMapper));
  40.     }
  41.     /**
  42.      * {@inheritdoc}
  43.      */
  44.     public function configureOptions(OptionsResolver $resolver)
  45.     {
  46.         parent::configureOptions($resolver);
  47.         // Constraint should always be converted to an array
  48.         $constraintsNormalizer = function (Options $options$constraints) {
  49.             return \is_object($constraints) ? [$constraints] : (array) $constraints;
  50.         };
  51.         $resolver->setDefaults([
  52.             'error_mapping' => [],
  53.             'constraints' => [],
  54.             'invalid_message' => 'This value is not valid.',
  55.             'invalid_message_parameters' => [],
  56.             'allow_extra_fields' => false,
  57.             'extra_fields_message' => 'This form should not contain extra fields.',
  58.         ]);
  59.         $resolver->setAllowedTypes('constraints', [Constraint::class, Constraint::class.'[]']);
  60.         $resolver->setNormalizer('constraints'$constraintsNormalizer);
  61.     }
  62.     /**
  63.      * {@inheritdoc}
  64.      */
  65.     public static function getExtendedTypes(): iterable
  66.     {
  67.         return [FormType::class];
  68.     }
  69. }