Skip to content

Commit 44f3a6e

Browse files
michallohniskydg
authored andcommitted
UploadControl: when file is uploaded with error, automatically shows error and isFilled() returns TRUE (BC break)
1 parent 497dcd6 commit 44f3a6e

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

src/Forms/Controls/UploadControl.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace Nette\Forms\Controls;
99

1010
use Nette;
11+
use Nette\Forms;
1112
use Nette\Http\FileUpload;
1213

1314

@@ -16,6 +17,8 @@
1617
*/
1718
class UploadControl extends BaseControl
1819
{
20+
/** validation rule */
21+
const VALID = ':uploadControlValid';
1922

2023
/**
2124
* @param string label
@@ -27,6 +30,8 @@ public function __construct($label = NULL, $multiple = FALSE)
2730
$this->control->type = 'file';
2831
$this->control->multiple = (bool) $multiple;
2932
$this->setOption('type', 'file');
33+
$this->addCondition(Forms\Form::FILLED)
34+
->addRule([$this, 'isOk'], Forms\Validator::$messages[self::VALID]);
3035
}
3136

3237

@@ -86,7 +91,9 @@ public function setValue($value)
8691
*/
8792
public function isFilled()
8893
{
89-
return $this->value instanceof FileUpload ? $this->value->isOk() : (bool) $this->value; // ignore NULL object
94+
return $this->value instanceof FileUpload
95+
? $this->value->getError() !== UPLOAD_ERR_NO_FILE // ignore NULL object
96+
: (bool) $this->value;
9097
}
9198

9299

src/Forms/Validator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class Validator extends Nette\Object
3939
Form::MIME_TYPE => 'The uploaded file is not in the expected format.',
4040
Form::IMAGE => 'The uploaded file must be image in format JPEG, GIF or PNG.',
4141
Controls\SelectBox::VALID => 'Please select a valid option.',
42+
Controls\UploadControl::VALID => 'An error occurred during file upload.',
4243
];
4344

4445

tests/Forms/Controls.UploadControl.loadData.phpt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ $_FILES = [
5252
'size' => [NULL],
5353
],
5454
'invalid2' => '',
55+
'partial' => [
56+
'name' => 'license.txt',
57+
'type' => 'text/plain',
58+
'tmp_name' => __DIR__ . '/files/logo.gif',
59+
'error' => UPLOAD_ERR_PARTIAL,
60+
'size' => 3013,
61+
],
5562
];
5663

5764

@@ -171,6 +178,24 @@ test(function () { // malformed data
171178
});
172179

173180

181+
test(function () { // partial uploaded (error)
182+
$form = new Form;
183+
$input = $form->addUpload('partial')
184+
->setRequired();
185+
186+
Assert::false($form->isValid());
187+
Assert::equal(new FileUpload([
188+
'name' => 'license.txt',
189+
'type' => '',
190+
'tmp_name' => __DIR__ . '/files/logo.gif',
191+
'error' => UPLOAD_ERR_PARTIAL,
192+
'size' => 3013,
193+
]), $input->getValue());
194+
Assert::true($input->isFilled());
195+
Assert::false($input->isOk());
196+
});
197+
198+
174199
test(function () { // validators
175200
$form = new Form;
176201
$input = $form->addUpload('avatar')

0 commit comments

Comments
 (0)