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 $validator;
  27.     private $violationMapper;
  28.     private $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.         $this->legacyErrorMessages $legacyErrorMessages;
  34.     }
  35.     /**
  36.      * {@inheritdoc}
  37.      */
  38.     public function buildForm(FormBuilderInterface $builder, array $options)
  39.     {
  40.         $builder->addEventSubscriber(new ValidationListener($this->validator$this->violationMapper));
  41.     }
  42.     /**
  43.      * {@inheritdoc}
  44.      */
  45.     public function configureOptions(OptionsResolver $resolver)
  46.     {
  47.         parent::configureOptions($resolver);
  48.         // Constraint should always be converted to an array
  49.         $constraintsNormalizer = function (Options $options$constraints) {
  50.             return \is_object($constraints) ? [$constraints] : (array) $constraints;
  51.         };
  52.         $resolver->setDefaults([
  53.             'error_mapping' => [],
  54.             'constraints' => [],
  55.             'invalid_message' => 'This value is not valid.',
  56.             'invalid_message_parameters' => [],
  57.             'legacy_error_messages' => $this->legacyErrorMessages,
  58.             'allow_extra_fields' => false,
  59.             'extra_fields_message' => 'This form should not contain extra fields.',
  60.         ]);
  61.         $resolver->setAllowedTypes('constraints', [Constraint::class, Constraint::class.'[]']);
  62.         $resolver->setAllowedTypes('legacy_error_messages''bool');
  63.         $resolver->setDeprecated('legacy_error_messages''symfony/form''5.2', function (Options $options$value) {
  64.             if (true === $value) {
  65.                 return 'Setting the "legacy_error_messages" option to "true" is deprecated. It will be disabled in Symfony 6.0.';
  66.             }
  67.             return '';
  68.         });
  69.         $resolver->setNormalizer('constraints'$constraintsNormalizer);
  70.     }
  71.     /**
  72.      * {@inheritdoc}
  73.      */
  74.     public static function getExtendedTypes(): iterable
  75.     {
  76.         return [FormType::class];
  77.     }
  78. }