8000 [Validator] Improve Image constraint invalid mime type message · symfony/symfony@0e66dbe · 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 0e66dbe

Browse files
committed
[Validator] Improve Image constraint invalid mime type message
1 parent 1027fad commit 0e66dbe

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
@@ -184,5 +184,9 @@ public function __construct(
184184
$this->allowLandscapeMessage = $allowLandscapeMessage ?? $this->allowLandscapeMessage;
185185
$this->allowPortraitMessage = $allowPortraitMessage ?? $this->allowPortraitMessage;
186186
$this->corruptedMessage = $corruptedMessage ?? $this->corruptedMessage;
187+
188+
if (!\in_array('image/*', (array) $this->mimeTypes, true) && !\array_key_exists('mimeTypesMessage', $options ?? []) && null === $mimeTypesMessage) {
189+
$this->mimeTypesMessage = 'The mime type of the file is invalid ({{ type }}). Allowed mime types are {{ types }}.';
190+
}
187191
}
188192
}

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

< 8000 div class="d-flex mr-2 flex-justify-end flex-items-center flex-1">
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,4 +511,36 @@ public function provideDetectCorruptedConstraints(): iterable
511511
new Image(detectCorrupted: true, corruptedMessage: 'myMessage'),
512512
];
513513
}
514+
515+
/**
516+
* @dataProvider provideInvalidMimeTypeWithNarrowedSet
517+
*/
518+
public function testInvalidMimeTypeWithNarrowedSet(Image $constraint)
519+
{
520+
$this->validator->validate($this->image, $constraint);
521+
522+
$this->buildViolation('The mime type of the file is invalid ({{ type }}). Allowed mime types are {{ types }}.')
523+
->setParameter('{{ file }}', sprintf('"%s"', $this->image))
524+
->setParameter('{{ type }}', '"image/gif"')
525+
->setParameter('{{ types }}', '"image/jpeg", "image/png"')
526+
->setParameter('{{ name }}', '"test.gif"')
527+
->setCode(Image::INVALID_MIME_TYPE_ERROR)
528+
->assertRaised();
529+
}
530+
531+
public function provideInvalidMimeTypeWithNarrowedSet()
532+
{
533+
yield 'Doctrine style' => [new Image([
534+
'mimeTypes' => [
535+
'image/jpeg',
536+
'image/png',
537+
],
538+
])];
539+
yield 'Named arguments' => [
540+
new Image(mimeTypes: [
541+
'image/jpeg',
542+
'image/png',
543+
]),
544+
];
545+
}
514546
}

0 commit comments

Comments
 (0)
0