• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nette / forms / 28608018984

02 Jul 2026 05:08PM UTC coverage: 93.512% (+0.2%) from 93.28%
28608018984

push

github

dg
x

2854 of 3052 relevant lines covered (93.51%)

0.94 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.56
/src/Forms/Controls/UploadControl.php
1
<?php declare(strict_types=1);
2

3
/**
4
 * This file is part of the Nette Framework (https://nette.org)
5
 * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6
 */
7

8
namespace Nette\Forms\Controls;
9

10
use Nette;
11
use Nette\Forms;
12
use Nette\Forms\Form;
13
use Nette\Forms\Validation;
14
use Nette\Http\FileUpload;
15
use Nette\Utils\Arrays;
16
use Stringable;
17

18

19
/**
20
 * Text box and browse button that allow users to select a file to upload to the server.
21
 */
22
class UploadControl extends BaseControl
23
{
24
        /** validation message keys */
25
        public const Valid = 'forms.upload';
26
        public const MaxFiles = 'forms.upload.maxFiles';
27

28
        #[\Deprecated('use UploadControl::Valid')]
29
        public const VALID = self::Valid;
30

31
        private bool $nullable = false;
32

33

34
        public function __construct(string|Stringable|null $label = null, bool $multiple = false)
1✔
35
        {
36
                parent::__construct($label);
1✔
37
                $this->control->type = 'file';
1✔
38
                $this->control->multiple = $multiple;
1✔
39
                $this->setOption('type', 'file');
1✔
40
                $this->addCondition(true) // not to block the export of rules to JS
1✔
41
                        ->addRule($this->isOk(...), new Validation\Message(self::Valid, 'An error occurred during file upload.'));
1✔
42
                $this->addRule(Form::MaxFileSize, null, Forms\Helpers::iniGetSize('upload_max_filesize'));
1✔
43
                if ($multiple) {
1✔
44
                        $maxFiles = (int) ini_get('max_file_uploads');
1✔
45
                        $this->addRule(
1✔
46
                                Form::MaxLength,
1✔
47
                                new Validation\Message(self::MaxFiles, 'The maximum allowed number of uploaded files is %count.', count: $maxFiles),
1✔
48
                                $maxFiles,
49
                        );
50
                }
51

52
                $this->monitor(Form::class, function (Form $form): void {
1✔
53
                        if (!$form->isMethod('post')) {
1✔
54
                                throw new Nette\InvalidStateException('File upload requires method POST.');
55
                        }
56

57
                        $form->getElementPrototype()->enctype = 'multipart/form-data';
1✔
58
                });
1✔
59
        }
1✔
60

61

62
        public function loadHttpData(): void
63
        {
64
                $this->value = $this->getSubmittedValue($this->control->multiple ? Form::DataList | Form::DataFile : Form::DataFile);
1✔
65
        }
1✔
66

67

68
        public function getHtmlName(): string
69
        {
70
                return parent::getHtmlName() . ($this->control->multiple ? '[]' : '');
1✔
71
        }
72

73

74
        /** @internal */
75
        public function setValue(mixed $value): static
1✔
76
        {
77
                return $this;
1✔
78
        }
79

80

81
        /**
82
         * Returns the uploaded file(s), a dummy FileUpload(null) when nothing was uploaded, or null when nullable is set.
83
         * @return FileUpload|FileUpload[]|null
84
         */
85
        public function getValue(): FileUpload|array|null
86
        {
87
                return $this->value ?? ($this->nullable ? null : new FileUpload(null));
1✔
88
        }
89

90

91
        /**
92
         * Has been any file uploaded?
93
         */
94
        public function isFilled(): bool
95
        {
96
                return (bool) $this->value;
1✔
97
        }
98

99

100
        /**
101
         * Sets whether getValue() returns null instead of FileUpload with error UPLOAD_ERR_NO_FILE.
102
         */
103
        public function setNullable(bool $value = true): static
1✔
104
        {
105
                $this->nullable = $value;
1✔
106
                return $this;
1✔
107
        }
108

109

110
        public function isNullable(): bool
111
        {
112
                return $this->nullable;
×
113
        }
114

115

116
        /**
117
         * Have been all files successfully uploaded?
118
         * @internal
119
         */
120
        public function isOk(): bool
121
        {
122
                return match (true) {
123
                        !$this->value => false,
1✔
124
                        is_array($this->value) => Arrays::every($this->value, fn(FileUpload $upload): bool => $upload->isOk()),
1✔
125
                        default => $this->value->isOk(),
1✔
126
                };
127
        }
128

129

130
        /** @param  Validation\Validator|Validation\ControlValidator|(callable(Nette\Forms\Control, mixed): bool)|string  $validator */
131
        public function addRule(
1✔
132
                Validation\Validator|Validation\ControlValidator|callable|string $validator,
133
                string|Stringable|null $errorMessage = null,
134
                mixed $arg = null,
135
        ): static
136
        {
137
                if ($validator === Form::MaxFileSize || $validator instanceof Validation\FileSizeValidator) {
1✔
138
                        $limit = $validator instanceof Validation\FileSizeValidator ? $validator->limit : $arg;
1✔
139
                        if ($limit > ($ini = Forms\Helpers::iniGetSize('upload_max_filesize'))) {
1✔
140
                                trigger_error("Value of MaxFileSize ($limit) is greater than value of directive upload_max_filesize ($ini).", E_USER_WARNING);
1✔
141
                        }
142
                        $this->getRules()->removeRule(Form::MaxFileSize);
1✔
143
                        $this->getRules()->removeRule(Validation\FileSizeValidator::class);
1✔
144

145
                } elseif ($validator === Form::MaxLength) {
1✔
146
                        if ($arg > ($ini = ini_get('max_file_uploads'))) {
1✔
147
                                trigger_error("Value of MaxLength ($arg) is greater than value of directive max_file_uploads ($ini).", E_USER_WARNING);
×
148
                        }
149
                        $this->getRules()->removeRule($validator);
1✔
150
                }
151

152
                return parent::addRule($validator, $errorMessage, $arg);
1✔
153
        }
154

155

156
        protected function getRuleAttributeNames(): array
157
        {
158
                return ['accept'];
1✔
159
        }
160
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc