Skip to content

Commit

Permalink
[Validator] FileValidator allow MIME with wildcard
Browse files Browse the repository at this point in the history
  • Loading branch information
stealth35 committed Sep 1, 2011
1 parent a9394de commit fff1456
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
24 changes: 21 additions & 3 deletions Constraints/FileValidator.php
Expand Up @@ -109,10 +109,28 @@ public function isValid($value, Constraint $constraint)
$value = new FileObject($value);
}

if (!in_array($value->getMimeType(), (array) $constraint->mimeTypes)) {
$mimeTypes = (array) $constraint->mimeTypes;
$mime = $value->getMimeType();
$valid = false;

foreach ($mimeTypes as $mimeType) {
if ($mimeType === $mime) {
$valid = true;
break;
}

if ($discrete = strstr($mimeType, '/*', true)) {
if (strstr($mime, '/', true) === $discrete) {
$valid = true;
break;
}
}
}

if (false === $valid) {
$this->setMessage($constraint->mimeTypesMessage, array(
'{{ type }}' => '"'.$value->getMimeType().'"',
'{{ types }}' => '"'.implode('", "', (array) $constraint->mimeTypes).'"',
'{{ type }}' => '"'.$mime.'"',
'{{ types }}' => '"'.implode('", "', $mimeTypes) .'"',
'{{ file }}' => $path,
));

Expand Down
8 changes: 1 addition & 7 deletions Constraints/Image.php
Expand Up @@ -18,13 +18,7 @@
*/
class Image extends File
{
public $mimeTypes = array(
'image/png',
'image/jpg',
'image/jpeg',
'image/pjpeg',
'image/gif',
);
public $mimeTypes = 'image/*';
public $mimeTypesMessage = 'This file is not a valid image';

/**
Expand Down

0 comments on commit fff1456

Please sign in to comment.