Repository: symfony/symfony
File: src/Symfony/Component/Form/Extension/Validator/Type/FormTypeValidatorExtension.php (PHP)

Code before:

namespace Symfony\Component\Form\Extension\Validator\Type;

use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\Extension\Validator\EventListener\ValidationListener;
use Symfony\Component\Form\Extension\Validator\ViolationMapper\ViolationMapper;
use Symfony\Component\Form\Extension\Validator\ViolationMapper\ViolationMapperInterface;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormRendererInterface;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
 * @author Bernhard Schussek <bschussek@gmail.com>
 */
class FormTypeValidatorExtension extends BaseValidatorExtension
{
    private bool $legacyErrorMessage = true;
    private ViolationMapperInterface $violationMapper;

    public function __construct(
        private ValidatorInterface $validator,
        bool|ViolationMapperInterface|null $legacyErrorMessagesOrViolationMapperInterface = true,
        ?FormRendererInterface $formRenderer = null,
        ?TranslatorInterface $translator = null,
    ) {
        if (\is_bool($legacyErrorMessagesOrViolationMapperInterface)) {
            trigger_deprecation('symfony/form', '8.1', \sprintf(
                'Not passing the "%s" as a second argument of the constructor is deprecated. It will be the default in Symfony 9.0.',
                ViolationMapperInterface::class
            ));
            $this->legacyErrorMessage = $legacyErrorMessagesOrViolationMapperInterface;
            $this->violationMapper = new ViolationMapper($formRenderer, $translator);
        } else {
            $this->legacyErrorMessage = false;
            $this->violationMapper = $legacyErrorMessagesOrViolationMapperInterface ?? new ViolationMapper($formRenderer, $translator);
        }
    }

    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder->addEventSubscriber(new ValidationListener($this->validator, $this->violationMapper));
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        parent::configureOptions($resolver);


Review comment (suggestion):
```suggestion
        bool|ViolationMapperInterface|null $violationMapper = null,
```

Code after:

namespace Symfony\Component\Form\Extension\Validator\Type;

use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\Extension\Validator\EventListener\ValidationListener;
use Symfony\Component\Form\Extension\Validator\ViolationMapper\ViolationMapper;
use Symfony\Component\Form\Extension\Validator\ViolationMapper\ViolationMapperInterface;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormRendererInterface;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
 * @author Bernhard Schussek <bschussek@gmail.com>
 */
class FormTypeValidatorExtension extends BaseValidatorExtension
{
    private readonly ViolationMapperInterface $violationMapper;

    public function __construct(
        private ValidatorInterface $validator,
        bool|ViolationMapperInterface|null $violationMapper = null,
        ?FormRendererInterface $formRenderer = null,
        ?TranslatorInterface $translator = null,
    ) {
        if (\is_bool($violationMapper)) {
            trigger_deprecation('symfony/form', '8.1', \sprintf('Passing a boolean as a second argument of "%s"\'s constructor is deprecated; pass a "%s" instead.', self::class, ViolationMapperInterface::class));
            $violationMapper = null;
        }
        $this->violationMapper = $violationMapper ?? new ViolationMapper($formRenderer, $translator);
    }

    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder->addEventSubscriber(new ValidationListener($this->validator, $this->violationMapper));
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        parent::configureOptions($resolver);

        // Constraint should always be converted to an array
        $constraintsNormalizer = static fn (Options $options, $constraints) => \is_object($constraints) ? [$constraints] : (array) $constraints;

        $resolver->setDefaults([
            'error_mapping' => [],
            'constraints' => [],
            'invalid_message' => 'This value is not valid.',