Skip to content

Commit fa96092

Browse files
committed
Merge pull request #86 from vchabot/multiple-file
Add Multiple file input
2 parents 012a346 + 6fe802e commit fa96092

File tree

6 files changed

+125
-1
lines changed

6 files changed

+125
-1
lines changed

lib/autoload/sfCoreAutoload.class.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,7 @@ static public function make()
480480
'sfvalidatorerror' => 'validator/sfValidatorError.class.php',
481481
'sfvalidatorerrorschema' => 'validator/sfValidatorErrorSchema.class.php',
482482
'sfvalidatorfile' => 'validator/sfValidatorFile.class.php',
483+
'sfvalidatorfilemulti' => 'validator/sfValidatorFileMulti.class.php',
483484
'sfvalidatorfromdescription' => 'validator/sfValidatorFromDescription.class.php',
484485
'sfvalidatorinteger' => 'validator/sfValidatorInteger.class.php',
485486
'sfvalidatorip' => 'validator/sfValidatorIp.class.php',
@@ -517,6 +518,7 @@ static public function make()
517518
'sfwidgetforminput' => 'widget/sfWidgetFormInput.class.php',
518519
'sfwidgetforminputcheckbox' => 'widget/sfWidgetFormInputCheckbox.class.php',
519520
'sfwidgetforminputfile' => 'widget/sfWidgetFormInputFile.class.php',
521+
'sfwidgetforminputfilemulti' => 'widget/sfWidgetFormInputFileMulti.class.php',
520522
'sfwidgetforminputfileeditable' => 'widget/sfWidgetFormInputFileEditable.class.php',
521523
'sfwidgetforminputhidden' => 'widget/sfWidgetFormInputHidden.class.php',
522524
'sfwidgetforminputpassword' => 'widget/sfWidgetFormInputPassword.class.php',
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
class sfValidatorFileMulti extends sfValidatorFile
4+
{
5+
6+
/**
7+
* @see sfValidatorBase
8+
*/
9+
protected function doClean($value)
10+
{
11+
$clean = array();
12+
13+
foreach ($value as $file)
14+
{
15+
$clean[] = parent::doClean($file);
16+
}
17+
18+
return $clean;
19+
}
20+
}

lib/widget/sfWidgetFormInputFile.class.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/*
44
* This file is part of the symfony package.
55
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
6-
*
6+
*
77
* For the full copyright and license information, please view the LICENSE
88
* file that was distributed with this source code.
99
*/
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/**
4+
* sfWidgetFormInputFileMulti represents an upload HTML input tag with multiple option.
5+
*
6+
* @package symfony
7+
* @subpackage widget
8+
* @author Vincent Chabot <vchabot@groupe-exp.com>
9+
* @version SVN: $Id$
10+
*/
11+
class sfWidgetFormInputFileMulti extends sfWidgetFormInputFile
12+
{
13+
/**
14+
* Configures the current widget.
15+
*
16+
* @param array $options An array of options
17+
* @param array $attributes An array of default HTML attributes
18+
*
19+
* @see sfWidgetFormInput
20+
*/
21+
protected function configure($options = array(), $attributes = array())
22+
{
23+
parent::configure($options, $attributes);
24+
25+
$this->addOption('multiple', true);
26+
}
27+
28+
/**
29+
* Renders the widget.
30+
*
31+
* @param string $name The element name
32+
* @param string $value The value displayed in this widget
33+
* @param array $attributes An array of HTML attributes to be merged with the default HTML attributes
34+
* @param array $errors An array of errors for the field
35+
*
36+
* @return string An HTML tag string
37+
*
38+
* @see sfWidgetForm
39+
*/
40+
public function render($name, $value = null, $attributes = array(), $errors = array())
41+
{
42+
$name .= $this->getOption('multiple') ? '[]' : '';
43+
return $this->renderTag('input', array_merge(array('type' => $this->getOption('type'), 'name' => $name, 'value' => $value, 'multiple' => $this->getOption('multiple')), $attributes));
44+
}
45+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the symfony package.
5+
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
require_once(__DIR__.'/../../bootstrap/unit.php');
12+
13+
$t = new lime_test(9);
14+
15+
$tmpDir = sys_get_temp_dir();
16+
$content = 'This is an ASCII file.';
17+
$content2 = 'This is an ASCII file. And another one.';
18+
file_put_contents($tmpDir.'/test.txt', $content);
19+
file_put_contents($tmpDir.'/test2.txt', $content2);
20+
21+
// ->clean()
22+
$t->diag('->clean()');
23+
$v = new sfValidatorFileMulti();
24+
25+
$f = $v->clean(array(
26+
array('tmp_name' => $tmpDir.'/test.txt'),
27+
array('tmp_name' => $tmpDir.'/test2.txt'),
28+
));
29+
30+
$t->ok(is_array($f), '->clean() returns an array of sfValidatedFile instances');
31+
32+
$t->ok($f[0] instanceof sfValidatedFile, '->clean() returns an array of sfValidatedFile');
33+
$t->is($f[0]->getOriginalName(), '', '->clean() returns an array of sfValidatedFile with an empty original name if the name is not passed in the initial value');
34+
$t->is($f[0]->getSize(), strlen($content), '->clean() returns an array of sfValidatedFile with a computed file size if the size is not passed in the initial value');
35+
$t->is($f[0]->getType(), 'text/plain', '->clean() returns an array of sfValidatedFile with a guessed content type');
36+
37+
$t->ok($f[1] instanceof sfValidatedFile, '->clean() returns an array of sfValidatedFile');
38+
$t->is($f[1]->getOriginalName(), '', '->clean() returns an array of sfValidatedFile with an empty original name if the name is not passed in the initial value');
39+
$t->is($f[1]->getSize(), strlen($content2), '->clean() returns an array of sfValidatedFile with a computed file size if the size is not passed in the initial value');
40+
$t->is($f[1]->getType(), 'text/plain', '->clean() returns an array of sfValidatedFile with a guessed content type');
41+
42+
unlink($tmpDir.'/test.txt');
43+
unlink($tmpDir.'/test2.txt');
44+
sfToolkit::clearDirectory($tmpDir.'/foo');
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
require_once(__DIR__.'/../../bootstrap/unit.php');
4+
5+
$t = new lime_test(1);
6+
7+
$w = new sfWidgetFormInputFileMulti();
8+
9+
// ->render()
10+
$t->diag('->render() multiple file upload');
11+
$t->is($w->render('foo'), '<input type="file" name="foo[]" multiple="1" id="foo" />', '->render() renders the widget as HTML');
12+
13+

0 commit comments

Comments
 (0)