Skip to content

Commit a059ec8

Browse files
Bernhard Schussekfabpot
authored andcommitted
[Validator] Implemented Image constraint
1 parent 993257a commit a059ec8

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function isValid($value, Constraint $constraint)
7777

7878
if ($constraint->mimeTypes) {
7979
if (!$value instanceof FileObject) {
80-
throw new ConstraintValidationException();
80+
$value = new FileObject($value);
8181
}
8282

8383
if (!in_array($value->getMimeType(), (array)$constraint->mimeTypes)) {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Symfony\Component\Validator\Constraints;
4+
5+
/*
6+
* This file is part of the Symfony framework.
7+
*
8+
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
9+
*
10+
* This source file is subject to the MIT license that is bundled
11+
* with this source code in the file LICENSE.
12+
*/
13+
14+
class Image extends File
15+
{
16+
public $mimeTypes = array(
17+
'image/png',
18+
'image/jpg',
19+
'image/jpeg',
20+
'image/pjpeg',
21+
'image/gif',
22+
);
23+
public $mimeTypesMessage = 'This file is not a valid image';
24+
25+
/**
26+
* @inheritDoc
27+
*/
28+
public function validatedBy()
29+
{
30+
return __NAMESPACE__.'\FileValidator';
31+
}
32+
}

0 commit comments

Comments
 (0)