Skip to content

Commit d16a708

Browse files
committed
[Form] simplified file type class
File uploads documentation is here: symfony/symfony-docs#400
1 parent 59f0602 commit d16a708

File tree

11 files changed

+25
-301
lines changed

11 files changed

+25
-301
lines changed

UPDATE.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,14 @@ beta4 to beta5
1919
methods in the Serializer class itself breaking BC and adding component
2020
specific Exception classes.
2121

22-
* The temporary storage for file uploads has been removed
22+
* The FileType Form class has been heavily changed:
23+
24+
* The temporary storage has been removed.
25+
26+
* The file type `type` option has also been removed (the new behavior is
27+
the same as when the `type` was set to `file` before).
28+
29+
* The file input is now rendered as any other input field.
2330

2431
* The `Symfony\Component\HttpFoundation\File\File::getExtension()` and
2532
`guessExtension()` methods do not return the extension with a `.` anymore.

src/Symfony/Bridge/Twig/Resources/views/Form/div_layout.html.twig

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,6 @@
1616
{% endspaceless %}
1717
{% endblock form_label %}
1818

19-
{% block file_label %}
20-
{% spaceless %}
21-
{{ form_label(form.file) }}
22-
{% endspaceless %}
23-
{% endblock file_label %}
24-
2519
{% block field_errors %}
2620
{% spaceless %}
2721
{% if errors|length > 0 %}
@@ -243,14 +237,6 @@
243237
{% endspaceless %}
244238
{% endblock percent_widget %}
245239

246-
{% block file_widget %}
247-
{% spaceless %}
248-
<div {{ block('container_attributes') }}>
249-
{{ form_widget(form.file) }}
250-
</div>
251-
{% endspaceless %}
252-
{% endblock file_widget %}
253-
254240
{% block repeated_row %}
255241
{% spaceless %}
256242
{{ block('field_rows') }}

src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/file_label.html.php

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
<div<?php echo $view['form']->attributes() ?>>
2-
<input type="file"
3-
id="<?php echo $view->escape($form['file']->get('id')) ?>"
4-
name="<?php echo $view->escape($form['file']->get('name')) ?>"
5-
<?php if ($form['file']->get('disabled')): ?>disabled="disabled"<?php endif ?>
6-
<?php if ($form['file']->get('required')): ?>required="required"<?php endif ?>
7-
/>
8-
</div>
1+
<input type="file"
2+
<?php echo $view['form']->attributes() ?>
3+
name="<?php echo $view->escape($full_name) ?>"
4+
value="<?php echo $view->escape($value) ?>"
5+
<?php if ($read_only): ?>disabled="disabled"<?php endif ?>
6+
<?php if ($required): ?>required="required"<?php endif ?>
7+
/>

src/Symfony/Component/Form/Extension/Core/DataTransformer/FileToArrayTransformer.php

Lines changed: 0 additions & 76 deletions
This file was deleted.

src/Symfony/Component/Form/Extension/Core/DataTransformer/FileToStringTransformer.php

Lines changed: 0 additions & 80 deletions
This file was deleted.

src/Symfony/Component/Form/Extension/Core/Type/FileType.php

Lines changed: 5 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -13,65 +13,25 @@
1313

1414
use Symfony\Component\Form\AbstractType;
1515
use Symfony\Component\Form\FormInterface;
16-
use Symfony\Component\Form\FormBuilder;
17-
use Symfony\Component\Form\ReversedTransformer;
18-
use Symfony\Component\Form\Extension\Core\DataTransformer\FileToStringTransformer;
19-
use Symfony\Component\Form\Extension\Core\DataTransformer\FileToArrayTransformer;
2016
use Symfony\Component\Form\FormView;
2117

2218
class FileType extends AbstractType
2319
{
2420
/**
2521
* {@inheritdoc}
2622
*/
27-
public function buildForm(FormBuilder $builder, array $options)
28-
{
29-
if ($options['type'] === 'string') {
30-
$builder->appendNormTransformer(
31-
new ReversedTransformer(new FileToStringTransformer())
32-
);
33-
}
34-
35-
$builder
36-
->appendNormTransformer(new FileToArrayTransformer())
37-
->add('file', 'field')
38-
;
39-
}
40-
41-
/**
42-
* {@inheritdoc}
43-
*/
44-
public function buildViewBottomUp(FormView $view, FormInterface $form)
23+
public function buildView(FormView $view, FormInterface $form)
4524
{
4625
$view
4726
->set('multipart', true)
48-
->getChild('file')
49-
->set('type', 'file')
50-
->set('value', '')
27+
->set('type', 'file')
28+
->set('value', '')
5129
;
5230
}
5331

54-
/**
55-
* {@inheritdoc}
56-
*/
57-
public function getDefaultOptions(array $options)
58-
{
59-
return array(
60-
'type' => 'string',
61-
);
62-
}
63-
64-
/**
65-
* {@inheritdoc}
66-
*/
67-
public function getAllowedOptionValues(array $options)
32+
public function getParent(array $options)
6833
{
69-
return array(
70-
'type' => array(
71-
'string',
72-
'file',
73-
),
74-
);
34+
return 'field';
7535
}
7636

7737
/**

tests/Symfony/Tests/Component/Form/AbstractDivLayoutTest.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -413,11 +413,8 @@ public function testFileLabelAccessibility()
413413
$this->assertMatchesXpath($html,
414414
'/div
415415
[
416-
./label[@for="name_file"]
417-
/following-sibling::div[@id="name"]
418-
[
419-
./input[@id="name_file"][@type="file"]
420-
]
416+
./label[@for="name"]
417+
/following-sibling::input[@id="name"][@type="file"]
421418
]
422419
'
423420
);

tests/Symfony/Tests/Component/Form/AbstractLayoutTest.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -753,11 +753,8 @@ public function testFile()
753753
));
754754

755755
$this->assertWidgetMatchesXpath($form->createView(), array(),
756-
'/div
757-
[
758-
./input[@type="file"][@id="na&me_file"]
759-
]
760-
[count(./input)=1]
756+
'/input
757+
[@type="file"]
761758
'
762759
);
763760
}

tests/Symfony/Tests/Component/Form/Extension/Core/DataTransformer/FileToStringTransformerTest.php

Lines changed: 0 additions & 65 deletions
This file was deleted.

tests/Symfony/Tests/Component/Form/Extension/Core/Type/FileTypeTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function testDontPassValueToView()
2525
));
2626
$view = $form->createView();
2727

28-
$this->assertEquals('', $view['file']->get('value'));
28+
$this->assertEquals('', $view->get('value'));
2929
}
3030

3131
private function createUploadedFileMock($name, $originalName, $valid)

0 commit comments

Comments
 (0)