Browse Source

[Form] Add exception for missing enctype attribute

Jordi Boggiano 14 years ago
parent
commit
88cfc4c011

+ 4 - 0
src/Symfony/Component/Form/FileField.php

@@ -13,6 +13,7 @@ namespace Symfony\Component\Form;
 
 use Symfony\Component\HttpFoundation\File\File;
 use Symfony\Component\Form\Exception\FormException;
+use Symfony\Component\HttpFoundation\File\UploadedFile;
 
 /**
  * A file field to upload files.
@@ -66,6 +67,9 @@ class FileField extends Form
     protected function preprocessData(array $data)
     {
         if ($data['file']) {
+            if (!$data['file'] instanceof UploadedFile) {
+                throw new \UnexpectedValueException('Uploaded file is not of type UploadedFile, your form tag is probably missing the enctype="multipart/form-data" attribute.');
+            }
             switch ($data['file']->getError()) {
                 case UPLOAD_ERR_INI_SIZE:
                     $this->iniSizeExceeded = true;

+ 12 - 0
tests/Symfony/Tests/Component/Form/FileFieldTest.php

@@ -111,6 +111,18 @@ class FileFieldTest extends \PHPUnit_Framework_TestCase
         $this->assertEquals(realpath($tmpPath), realpath($this->field->getData()));
     }
 
+    /**
+     * @expectedException UnexpectedValueException
+     */
+    public function testSubmitFailsOnMissingMultipart()
+    {
+        $this->field->submit(array(
+            'file' => 'foo.jpg',
+            'token' => '12345',
+            'original_name' => 'original_name.jpg',
+        ));
+    }
+
     public function testSubmitKeepsOldFileIfNotOverwritten()
     {
         $oldPath = tempnam(sys_get_temp_dir(), 'FileFieldTest');