8000 bug #53538 [Validator] Fix charset encoding detection in `CharsetVali… · symfony/symfony@b40b65d · GitHub
[go: up one dir, main page]

Skip to content

Commit b40b65d

Browse files
committed
bug #53538 [Validator] Fix charset encoding detection in CharsetValidator (alexandre-daubois)
This PR was merged into the 7.1 branch. Discussion ---------- [Validator] Fix charset encoding detection in `CharsetValidator` | Q | A | ------------- | --- | Branch? | 7.1 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix #53154 (comment) | License | MIT After `@smnandre` suggestion, I updated the constraint to display a better message on the detected encoding. Indeed, if we fail to check the encoding is in the provided ones, then we fall back on any encoding detected by mbstring. Commits ------- 6393e29 [Validator] Fix charset encoding detection in `CharsetValidator`
2 parents 744d0ac + 6393e29 commit b40b65d

File tree

2 files changed

+3
-3
lines changed

2 files changed

+3
-3
lines changed

src/Symfony/Component/Validator/Constraints/CharsetValidator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ public function validate(mixed $value, Constraint $constraint): void
3535
throw new UnexpectedValueException($value, 'string');
3636
}
3737

38-
if (!\in_array($detected = mb_detect_encoding($value, $constraint->encodings, true), (array) $constraint->encodings, true)) {
38+
if (!\in_array(mb_detect_encoding($value, $constraint->encodings, true), (array) $constraint->encodings, true)) {
3939
$this->context->buildViolation($constraint->message)
40-
->setParameter('{{ detected }}', $detected)
40+
->setParameter('{{ detected }}', mb_detect_encoding($value, strict: true))
4141
->setParameter('{{ encodings }}', implode(', ', $constraint->encodings))
4242
->setCode(Charset::BAD_ENCODING_ERROR)
4343
->addViolation();

src/Symfony/Component/Validator/Tests/Constraints/CharsetValidatorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function testInvalidValues(string $value, array $encodings)
4242
$this->validator->validate($value, new Charset(encodings: $encodings));
4343

4444
$this->buildViolation('The detected character encoding is invalid ({{ detected }}). Allowed encodings are {{ encodings }}.')
45-
->setParameter('{{ detected }}', mb_detect_encoding($value, $encodings, true))
45+
->setParameter('{{ detected }}', 'UTF-8')
4646
->setParameter('{{ encodings }}', implode(', ', $encodings))
4747
->setCode(Charset::BAD_ENCODING_ERROR)
4848
->assertRaised();

0 commit comments

Comments
 (0)
0