Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Email-Validator reports incorrect deprecation warning #48944

Closed
@DigitalTimK

Description

@DigitalTimK

Symfony version(s) affected

6.2.4

Description

Since my change to symfony 6.2 I get a deprecation warning on the EMail validator:
Since symfony/validator 6.2: The "loose" mode is deprecated. The default mode will be changed to "html5" in 7.0.

In my opinion this message is wrong (and should not be raised) and can also not be solved by code-adaptation. Something else must causing this false warning.

What i did so far:

  • If I remove the TROUBLEMAKER-section, the deprecation warning is gone. So this is causing the issue in my opinion
  • If I add the MODE-option to the TROUBLEMAKER-section, I have no effect and the deprecation warning remains:
new Assert\Email([
  'message' => 'That email address looks wrong',
  'mode' => 'html5'
]),

How to reproduce

# config/packages/validator.yaml

framework:
    validation:
        email_validation_mode: html5

        # Enables validator auto-mapping support.
        # For instance, basic validation constraints will be inferred from Doctrine's metadata.
        #auto_mapping:
        #    App\Entity\: []

when@test:
    framework:
        validation:
            not_compromised_password: false
<?php

namespace App\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Symfony\Component\Validator\Validation;

class BookingCustomerTypeValidationFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('customerType', ChoiceType::class, [
                'expanded' => true,
                'multiple' => false,
                'label'    => 'Do we know you?',
                'choices'  => [
                    'no'  => 'customer_new',
                    'yes' => 'customer_existing',
                ],
                'constraints' => [
                    new Assert\NotBlank([
                        'message' => 'Please make a choice',
                    ]),
                ],
            ])
            ->add('existingCustomersEmail', EmailType::class, [
                'label'       => 'Your email address',
                'constraints' => [],
            ]);
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'constraints' => [
                new Assert\Callback($this->validate(...)),
            ],
        ]);
    }

    public function validate(array $data, ExecutionContextInterface $context): void
    {
        $constraints = match ($data['customerType']) {
            'customer_new' => new Assert\Collection([
                'customerType'           => null,
                'existingCustomersEmail' => new Assert\Blank([
                    'message' => 'This field must be blank if you are a new customer',
                ]),
            ]),
            'customer_existing' => new Assert\Collection([
                'customerType'           => null,
                'existingCustomersEmail' => new Assert\Required([

                    // Troublemaker: Start
                    new Assert\Email([
                        'message' => 'That email address looks wrong',
                    ]),
                    // Troublemaker: End

                    new Assert\NotBlank([
                        'message' => 'This field must not be blank if you are an existing customer',
                    ]),
                ]),
            ]),
            default => new Assert\Collection([
                'customerType' => new Assert\Blank([ // blank should always a violation in this case
                    'message' => 'Impossible choice',
                ]),
                'existingCustomersEmail' => null,
            ]),
        };

        $validator = Validation::createValidator();
        $errors    = $validator->validate($data, $constraints);

        /** @var ConstraintViolation $error */
        foreach ($errors as $error) {
            $context->buildViolation($error->getMessage())
                ->atPath($error->getPropertyPath())
                ->addViolation();
        }
    }
}

Possible Solution

No response

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions