Description
Symfony version(s) affected
6.2
Description
I'm using the standalone symfony/validator
package, without Symfony. Since I updated from PHPUnit 9 to PHPUnit 10, the following warnings were displayed:
Since symfony/validator 6.2: The "loose" mode is deprecated. The default mode will be changed to "html5" in 7.0.
How to reproduce
Here is a minimal example (TestCase) to reproduce the issue:
<?php
namespace App\Test;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Validation;
class ValidatorTest extends TestCase
{
public function test1()
{
$validator = Validation::createValidator();
$violations = $validator->validate('user@example.com', [
new Email(),
]);
$this->assertEmpty($violations);
}
public function test2()
{
$validator = Validation::createValidator();
$violations = $validator->validate('user@example.com', [
new Email(null, null, Email::VALIDATION_MODE_HTML5),
]);
$this->assertEmpty($violations);
}
}
The second test case still triggers the same deprecation warning in PHPUnit 10, even if I pass the validation mode explicitly.
To display the deprecation details, I'm using the --display-deprecations
as parameter, e.g.
phpunit --configuration phpunit.xml --do-not-cache-result --colors=always --display-deprecations
Possible Solution
I figured out that the ConstraintValidatorFactory
creates an array of Constraint using this syntax:
return $this->validators[$name] ??= new $class();
So when the class is Symfony\Component\Validator\Constraints\EmailValidator
, then it creates an object of the EmailValidator
without passing any constructor parameters. This means the default Email::VALIDATION_MODE_LOOSE
value will be used instead of the given Email::VALIDATION_MODE_HTML5
value.
- https://github.com/symfony/validator/blob/6.2/ConstraintValidatorFactory.php#L40
- https://github.com/symfony/validator/blob/6.2/Constraints/EmailValidator.php#L40
- https://github.com/symfony/validator/blob/6.2/Constraints/EmailValidator.php#L47
I have no idea how to fix this.
Additional Context
No response