[Form] simplified file type class

File uploads documentation is here:

https://github.com/symfony/symfony-docs/pull/400
This commit is contained in:
Fabien Potencier 2011-06-13 08:14:41 +02:00
parent 59f0602aef
commit d16a708cc8
11 changed files with 25 additions and 301 deletions

View File

@ -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.

View File

@ -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 %}
<div {{ block('container_attributes') }}>
{{ form_widget(form.file) }}
</div>
{% endspaceless %}
{% endblock file_widget %}
{% block repeated_row %}
{% spaceless %}
{{ block('field_rows') }}

View File

@ -1 +0,0 @@
<?php echo $view['form']->label($form['file']) ?>

View File

@ -1,8 +1,7 @@
<div<?php echo $view['form']->attributes() ?>>
<input type="file"
id="<?php echo $view->escape($form['file']->get('id')) ?>"
name="<?php echo $view->escape($form['file']->get('name')) ?>"
<?php if ($form['file']->get('disabled')): ?>disabled="disabled"<?php endif ?>
<?php if ($form['file']->get('required')): ?>required="required"<?php endif ?>
/>
</div>
<input type="file"
<?php echo $view['form']->attributes() ?>
name="<?php echo $view->escape($full_name) ?>"
value="<?php echo $view->escape($value) ?>"
<?php if ($read_only): ?>disabled="disabled"<?php endif ?>
<?php if ($required): ?>required="required"<?php endif ?>
/>

View File

@ -1,76 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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 <bernhard.schussek@symfony-project.com>
*/
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'];
}
}

View File

@ -1,80 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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 <bernhard.schussek@symfony-project.com>
*/
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;
}
}

View File

@ -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';
}
/**

View File

@ -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"]
]
'
);

View File

@ -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"]
'
);
}

View File

@ -1,65 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* 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');
}
}

View File

@ -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)