diff --git a/Constraints/FileValidator.php b/Constraints/FileValidator.php index 90ab8c8d2..4c0b3a3f8 100644 --- a/Constraints/FileValidator.php +++ b/Constraints/FileValidator.php @@ -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, )); diff --git a/Constraints/Image.php b/Constraints/Image.php index e9005b3ce..b9249dc7d 100644 --- a/Constraints/Image.php +++ b/Constraints/Image.php @@ -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'; /**