Browse Source

[Validator] Implemented Image constraint

Bernhard Schussek 14 years ago
parent
commit
a059ec891d

+ 1 - 1
src/Symfony/Component/Validator/Constraints/FileValidator.php

@@ -77,7 +77,7 @@ class FileValidator extends ConstraintValidator
 
         if ($constraint->mimeTypes) {
             if (!$value instanceof FileObject) {
-                throw new ConstraintValidationException();
+                $value = new FileObject($value);
             }
 
             if (!in_array($value->getMimeType(), (array)$constraint->mimeTypes)) {

+ 32 - 0
src/Symfony/Component/Validator/Constraints/Image.php

@@ -0,0 +1,32 @@
+<?php
+
+namespace Symfony\Component\Validator\Constraints;
+
+/*
+ * This file is part of the Symfony framework.
+ *
+ * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this source code in the file LICENSE.
+ */
+
+class Image extends File
+{
+    public $mimeTypes = array(
+        'image/png',
+        'image/jpg',
+        'image/jpeg',
+        'image/pjpeg',
+        'image/gif',
+    );
+    public $mimeTypesMessage = 'This file is not a valid image';
+
+    /**
+     * @inheritDoc
+     */
+    public function validatedBy()
+    {
+        return __NAMESPACE__.'\FileValidator';
+    }
+}