diff --git a/UPDATE.md b/UPDATE.md index 906b62ed1e..5a3f08ecce 100644 --- a/UPDATE.md +++ b/UPDATE.md @@ -19,7 +19,14 @@ beta4 to beta5 methods in the Serializer class itself breaking BC and adding component specific Exception classes. -* The temporary storage for file uploads has been removed +* The FileType Form class has been heavily changed: + + * The temporary storage has been removed. + + * The file type `type` option has also been removed (the new behavior is + the same as when the `type` was set to `file` before). + + * The file input is now rendered as any other input field. * The `Symfony\Component\HttpFoundation\File\File::getExtension()` and `guessExtension()` methods do not return the extension with a `.` anymore. diff --git a/src/Symfony/Bridge/Twig/Resources/views/Form/div_layout.html.twig b/src/Symfony/Bridge/Twig/Resources/views/Form/div_layout.html.twig index afb35fa60d..f24ae2055d 100644 --- a/src/Symfony/Bridge/Twig/Resources/views/Form/div_layout.html.twig +++ b/src/Symfony/Bridge/Twig/Resources/views/Form/div_layout.html.twig @@ -16,12 +16,6 @@ {% endspaceless %} {% endblock form_label %} -{% block file_label %} -{% spaceless %} - {{ form_label(form.file) }} -{% endspaceless %} -{% endblock file_label %} - {% block field_errors %} {% spaceless %} {% if errors|length > 0 %} @@ -243,14 +237,6 @@ {% endspaceless %} {% endblock percent_widget %} -{% block file_widget %} -{% spaceless %} -
- {{ form_widget(form.file) }} -
-{% endspaceless %} -{% endblock file_widget %} - {% block repeated_row %} {% spaceless %} {{ block('field_rows') }} diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/file_label.html.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/file_label.html.php deleted file mode 100644 index 80177b39df..0000000000 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/file_label.html.php +++ /dev/null @@ -1 +0,0 @@ -label($form['file']) ?> diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/file_widget.html.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/file_widget.html.php index 51b985b4f7..2e07bb135b 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/file_widget.html.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/file_widget.html.php @@ -1,8 +1,7 @@ -attributes() ?>> - get('disabled')): ?>disabled="disabled" - get('required')): ?>required="required" - /> - +attributes() ?> + name="escape($full_name) ?>" + value="escape($value) ?>" + disabled="disabled" + required="required" +/> diff --git a/src/Symfony/Component/Form/Extension/Core/DataTransformer/FileToArrayTransformer.php b/src/Symfony/Component/Form/Extension/Core/DataTransformer/FileToArrayTransformer.php deleted file mode 100644 index 58c0e0f3b2..0000000000 --- a/src/Symfony/Component/Form/Extension/Core/DataTransformer/FileToArrayTransformer.php +++ /dev/null @@ -1,76 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Form\Extension\Core\DataTransformer; - -use Symfony\Component\Form\DataTransformerInterface; -use Symfony\Component\Form\Exception\TransformationFailedException; -use Symfony\Component\Form\Exception\UnexpectedTypeException; -use Symfony\Component\HttpFoundation\File\File; - -/** - * @author Bernhard Schussek - */ -class FileToArrayTransformer implements DataTransformerInterface -{ - /** - * Convert a File instance to a file representation - * - * @param File $file The file - * - * @return array The file representation - * - * @throws UnexpectedTypeException if the file is not an instance of File - */ - public function transform($file) - { - if (null === $file || '' === $file) { - return array('file' => ''); - } - - if (!$file instanceof File) { - throw new UnexpectedTypeException($file, 'Symfony\Component\HttpFoundation\File\File'); - } - - return array('file' => $file); - } - - /** - * Transform a file internal representation to a File instance - * - * @param File $array the file representation - * - * @return File The file - * - * @throws UnexpectedTypeException if the file representation is not an array - * @throws TransformationFailedException if the file representation is invalid - */ - public function reverseTransform($array) - { - if (null === $array || '' === $array || array() === $array) { - return null; - } - - if (!is_array($array)) { - throw new UnexpectedTypeException($array, 'array'); - } - - if (!array_key_exists('file', $array)) { - throw new TransformationFailedException('The key "file" is missing.'); - } - - if (!empty($array['file']) && !$array['file'] instanceof File) { - throw new TransformationFailedException('The key "file" should be empty or instance of File (Have you set the "enctype" attribute of the form tag to "multipart/form-data"?).'); - } - - return $array['file']; - } -} diff --git a/src/Symfony/Component/Form/Extension/Core/DataTransformer/FileToStringTransformer.php b/src/Symfony/Component/Form/Extension/Core/DataTransformer/FileToStringTransformer.php deleted file mode 100644 index 31d429583c..0000000000 --- a/src/Symfony/Component/Form/Extension/Core/DataTransformer/FileToStringTransformer.php +++ /dev/null @@ -1,80 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Form\Extension\Core\DataTransformer; - -use Symfony\Component\Form\DataTransformerInterface; -use Symfony\Component\Form\Exception\UnexpectedTypeException; -use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException; -use Symfony\Component\Form\Exception\TransformationFailedException; -use Symfony\Component\HttpFoundation\File\File; - -/** - * @author Bernhard Schussek - */ -class FileToStringTransformer implements DataTransformerInterface -{ - /** - * Transforms a File instance to a path - * - * @param File $file The file - * - * @return string The path to the file - * - * @throws UnexpectedTypeException if the given file is not an instance of File - */ - public function transform($file) - { - if (null === $file || '' === $file) { - return ''; - } - - if (!$file instanceof File) { - throw new UnexpectedTypeException($file, 'Symfony\Component\HttpFoundation\File\File'); - } - - return $file->getPath(); - } - - - /** - * Transforms a path to a File instance - * - * @param string $path The path to the file - * - * @return File The File - * - * @throws UnexpectedTypeException if the given path is not a string - * @throws TransformationFailedException if the File instance could not be created - */ - public function reverseTransform($path) - { - if (null === $path || '' === $path) { - return null; - } - - if (!is_string($path)) { - throw new UnexpectedTypeException($path, 'string'); - } - - try { - $file = new File($path); - } catch (FileNotFoundException $e) { - throw new TransformationFailedException( - sprintf('The file "%s" does not exist', $path), - $e->getCode(), - $e - ); - } - - return $file; - } -} diff --git a/src/Symfony/Component/Form/Extension/Core/Type/FileType.php b/src/Symfony/Component/Form/Extension/Core/Type/FileType.php index 43f07bf044..30f37203b2 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/FileType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/FileType.php @@ -13,10 +13,6 @@ namespace Symfony\Component\Form\Extension\Core\Type; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormInterface; -use Symfony\Component\Form\FormBuilder; -use Symfony\Component\Form\ReversedTransformer; -use Symfony\Component\Form\Extension\Core\DataTransformer\FileToStringTransformer; -use Symfony\Component\Form\Extension\Core\DataTransformer\FileToArrayTransformer; use Symfony\Component\Form\FormView; class FileType extends AbstractType @@ -24,54 +20,18 @@ class FileType extends AbstractType /** * {@inheritdoc} */ - public function buildForm(FormBuilder $builder, array $options) - { - if ($options['type'] === 'string') { - $builder->appendNormTransformer( - new ReversedTransformer(new FileToStringTransformer()) - ); - } - - $builder - ->appendNormTransformer(new FileToArrayTransformer()) - ->add('file', 'field') - ; - } - - /** - * {@inheritdoc} - */ - public function buildViewBottomUp(FormView $view, FormInterface $form) + public function buildView(FormView $view, FormInterface $form) { $view ->set('multipart', true) - ->getChild('file') - ->set('type', 'file') - ->set('value', '') + ->set('type', 'file') + ->set('value', '') ; } - /** - * {@inheritdoc} - */ - public function getDefaultOptions(array $options) + public function getParent(array $options) { - return array( - 'type' => 'string', - ); - } - - /** - * {@inheritdoc} - */ - public function getAllowedOptionValues(array $options) - { - return array( - 'type' => array( - 'string', - 'file', - ), - ); + return 'field'; } /** diff --git a/tests/Symfony/Tests/Component/Form/AbstractDivLayoutTest.php b/tests/Symfony/Tests/Component/Form/AbstractDivLayoutTest.php index fc1353772d..2ffca0b624 100644 --- a/tests/Symfony/Tests/Component/Form/AbstractDivLayoutTest.php +++ b/tests/Symfony/Tests/Component/Form/AbstractDivLayoutTest.php @@ -413,11 +413,8 @@ abstract class AbstractDivLayoutTest extends AbstractLayoutTest $this->assertMatchesXpath($html, '/div [ - ./label[@for="name_file"] - /following-sibling::div[@id="name"] - [ - ./input[@id="name_file"][@type="file"] - ] + ./label[@for="name"] + /following-sibling::input[@id="name"][@type="file"] ] ' ); diff --git a/tests/Symfony/Tests/Component/Form/AbstractLayoutTest.php b/tests/Symfony/Tests/Component/Form/AbstractLayoutTest.php index 5df06eab4a..179a1bd894 100644 --- a/tests/Symfony/Tests/Component/Form/AbstractLayoutTest.php +++ b/tests/Symfony/Tests/Component/Form/AbstractLayoutTest.php @@ -753,11 +753,8 @@ abstract class AbstractLayoutTest extends \PHPUnit_Framework_TestCase )); $this->assertWidgetMatchesXpath($form->createView(), array(), -'/div - [ - ./input[@type="file"][@id="na&me_file"] - ] - [count(./input)=1] +'/input + [@type="file"] ' ); } diff --git a/tests/Symfony/Tests/Component/Form/Extension/Core/DataTransformer/FileToStringTransformerTest.php b/tests/Symfony/Tests/Component/Form/Extension/Core/DataTransformer/FileToStringTransformerTest.php deleted file mode 100644 index 1a400c643c..0000000000 --- a/tests/Symfony/Tests/Component/Form/Extension/Core/DataTransformer/FileToStringTransformerTest.php +++ /dev/null @@ -1,65 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Tests\Component\Form\Extension\Core\DataTransformer; - -use Symfony\Component\Form\Extension\Core\DataTransformer\FileToStringTransformer; -use Symfony\Component\HttpFoundation\File\File; - -class FileToStringTransformerTest extends \PHPUnit_Framework_TestCase -{ - private $transformer; - - protected function setUp() - { - $this->transformer = new FileToStringTransformer(); - } - - public function testTransform() - { - $path = realpath(__DIR__.'/../../../Fixtures/foo'); - - $file = new File($path); - $t = $this->transformer->transform($file); - - $this->assertTrue(file_exists($path)); - $this->assertInternalType('string', $t); - $this->assertEquals($path, realpath($t)); - } - - /** - * @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException - */ - public function testTransformRequiresAFile() - { - $this->transformer->transform(array()); - } - - public function testReverseTransform() - { - $path = realpath(__DIR__.'/../../../Fixtures/foo'); - - $file = new File($path); - $r = $this->transformer->reverseTransform($path); - - $this->assertInstanceOf('Symfony\Component\HttpFoundation\File\File', $file); - $this->assertEquals($path, realpath($r->getPath())); - - } - - /** - * @expectedException Symfony\Component\Form\Exception\TransformationFailedException - */ - public function testReverseTransformRequiresArray() - { - $t = $this->transformer->reverseTransform(__DIR__.'/../../../Fixtures/no-foo'); - } -} diff --git a/tests/Symfony/Tests/Component/Form/Extension/Core/Type/FileTypeTest.php b/tests/Symfony/Tests/Component/Form/Extension/Core/Type/FileTypeTest.php index 849cc37c8a..87f46f7819 100644 --- a/tests/Symfony/Tests/Component/Form/Extension/Core/Type/FileTypeTest.php +++ b/tests/Symfony/Tests/Component/Form/Extension/Core/Type/FileTypeTest.php @@ -25,7 +25,7 @@ class FileTypeTest extends TypeTestCase )); $view = $form->createView(); - $this->assertEquals('', $view['file']->get('value')); + $this->assertEquals('', $view->get('value')); } private function createUploadedFileMock($name, $originalName, $valid)