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

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ef6be5e

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

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

0 commit comments

Comments
 (0)
0