8000 [Validator] Improve Image constraint invalid mime type message · symfony/symfony@b82a145 · GitHub
[go: up one dir, main page]

Skip to content

Commit b82a145

Browse files
committed
[Validator] Improve Image constraint invalid mime type message
1 parent e6e7efd commit b82a145

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,5 +189,9 @@ public function __construct(
189189
$this->allowLandscapeMessage = $allowLandscapeMessage ?? $this->allowLandscapeMessage;
190190
$this->allowPortraitMessage = $allowPortraitMessage ?? $this->allowPortraitMessage;
191191
$this->corruptedMessage = $corruptedMessage ?? $this->corruptedMessage;
192+
193+
if (!\in_array('image/*', (array) $this->mimeTypes, true) && !\array_key_exists('mimeTypesMessage', $options ?? []) && null === $mimeTypesMessage) {
194+
$this->mimeTypesMessage = 'The mime type of the file is invalid ({{ type }}). Allowed mime types are {{ types }}.';
195+
}
192196
}
193197
}

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,4 +547,36 @@ public function provideDetectCorruptedConstraints(): iterable
547547
new Image(detectCorrupted: true, corruptedMessage: 'myMessage'),
548548
];
549549
}
550+
551+
/**
552+
* @dataProvider provideInvalidMimeTypeWithNarrowedSet
553+
*/
554+
public function testInvalidMimeTypeWithNarrowedSet(Image $constraint)
555+
{
556+
$this->validator->validate($this->image, $constraint);
557+
558+
$this->buildViolation('The mime type of the file is invalid ({{ type }}). Allowed mime types are {{ types }}.')
559+
->setParameter('{{ file }}', sprintf('"%s"', $this->image))
560+
->setParameter('{{ type }}', '"image/gif"')
561+
->setParameter('{{ types }}', '"image/jpeg", "image/png"')
562+
->setParameter('{{ name }}', '"test.gif"')
563+
->setCode(Image::INVALID_MIME_TYPE_ERROR)
564+
->assertRaised();
565+
}
566+
567+
public function provideInvalidMimeTypeWithNarrowedSet()
568+
{
569+
yield 'Doctrine style' => [new Image([
570+
'mimeTypes' => [
571+
'image/jpeg',
572+
'image/png',
573+
],
574+
])];
575+
yield 'Named arguments' => [
576+
new Image(mimeTypes: [
577+
'image/jpeg',
578+
'image/png',
579+
]),
580+
];
581+
}
550582
}

0 commit comments

Comments
 (0)
0