-
-
Notifications
You must be signed in to change notification settings - Fork 147
Expand file tree
/
Copy pathUploadControl.php
More file actions
109 lines (89 loc) · 2.3 KB
/
UploadControl.php
File metadata and controls
109 lines (89 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/
declare(strict_types=1);
namespace Nette\Forms\Controls;
use Nette;
use Nette\Forms;
use Nette\Http\FileUpload;
/**
* Text box and browse button that allow users to select a file to upload to the server.
*/
class UploadControl extends BaseControl
{
/** validation rule */
public const VALID = ':uploadControlValid';
/**
* @param string|object
*/
public function __construct($label = null, bool $multiple = false)
{
parent::__construct($label);
$this->control->type = 'file';
$this->control->multiple = $multiple;
$this->setOption('type', 'file');
$this->addCondition(Forms\Form::FILLED)
->addRule([$this, 'isOk'], Forms\Validator::$messages[self::VALID]);
}
/**
* This method will be called when the component (or component's parent)
* becomes attached to a monitored object. Do not call this method yourself.
*/
protected function attached(Nette\ComponentModel\IComponent $form): void
{
if ($form instanceof Nette\Forms\Form) {
if (!$form->isMethod('post')) {
throw new Nette\InvalidStateException('File upload requires method POST.');
}
$form->getElementPrototype()->enctype = 'multipart/form-data';
}
parent::attached($form);
}
/**
* Loads HTTP data.
*/
public function loadHttpData(): void
{
$this->value = $this->getHttpData(Nette\Forms\Form::DATA_FILE);
if ($this->value === null) {
$this->value = new FileUpload(null);
}
}
/**
* Returns HTML name of control.
*/
public function getHtmlName(): string
{
return parent::getHtmlName() . ($this->control->multiple ? '[]' : '');
}
/**
* @return static
* @internal
*/
public function setCurrentValue($value)
{
return $this;
}
/**
* Has been any file uploaded?
*/
public function isFilled(): bool
{
return $this->value instanceof FileUpload
? $this->value->getError() !== UPLOAD_ERR_NO_FILE // ignore null object
: (bool) $this->value;
}
/**
* Have been all files succesfully uploaded?
*/
public function isOk(): bool
{
return $this->value instanceof FileUpload
? $this->value->isOk()
: $this->value && array_reduce($this->value, function ($carry, $fileUpload) {
return $carry && $fileUpload->isOk();
}, true);
}
}