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

nette / forms / 26452888331

26 May 2026 02:01PM UTC coverage: 93.307% (+0.07%) from 93.241%
26452888331

push

github

dg
added CLAUDE.md

2105 of 2256 relevant lines covered (93.31%)

0.93 hits per line

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

92.86
/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\Http\FileUpload;
14
use Nette\Utils\Arrays;
15
use Stringable;
16
use function implode, ini_get, is_array;
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 rule */
25
        public const Valid = ':uploadControlValid';
26

27
        /** @deprecated use UploadControl::Valid */
28
        public const VALID = self::Valid;
29

30
        private bool $nullable = false;
31

32

33
        public function __construct(string|Stringable|null $label = null, bool $multiple = false)
1✔
34
        {
35
                parent::__construct($label);
1✔
36
                $this->control->type = 'file';
1✔
37
                $this->control->multiple = $multiple;
1✔
38
                $this->setOption('type', 'file');
1✔
39
                $this->addCondition(true) // not to block the export of rules to JS
1✔
40
                        ->addRule($this->isOk(...), Forms\Validator::$messages[self::Valid]);
1✔
41
                $this->addRule(Form::MaxFileSize, null, Forms\Helpers::iniGetSize('upload_max_filesize'));
1✔
42
                if ($multiple) {
1✔
43
                        $this->addRule(Form::MaxLength, 'The maximum allowed number of uploaded files is %d', (int) ini_get('max_file_uploads'));
1✔
44
                }
45

46
                $this->monitor(Form::class, function (Form $form): void {
1✔
47
                        if (!$form->isMethod('post')) {
1✔
48
                                throw new Nette\InvalidStateException('File upload requires method POST.');
49
                        }
50

51
                        $form->getElementPrototype()->enctype = 'multipart/form-data';
1✔
52
                });
1✔
53
        }
1✔
54

55

56
        public function loadHttpData(): void
57
        {
58
                $this->value = $this->getHttpData(Form::DataFile);
1✔
59
        }
1✔
60

61

62
        public function getHtmlName(): string
63
        {
64
                return parent::getHtmlName() . ($this->control->multiple ? '[]' : '');
1✔
65
        }
66

67

68
        /**
69
         * @return static
70
         * @internal
71
         */
72
        public function setValue($value)
73
        {
74
                return $this;
1✔
75
        }
76

77

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

87

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

96

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

106

107
        public function isNullable(): bool
108
        {
109
                return $this->nullable;
×
110
        }
111

112

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

126

127
        /**
128
         * @param  (callable(Nette\Forms\Control): bool)|string  $validator
129
         * @return static
130
         */
131
        public function addRule(
1✔
132
                callable|string $validator,
133
                string|Stringable|null $errorMessage = null,
134
                mixed $arg = null,
135
        ) {
136
                if ($validator === Form::Image) {
1✔
137
                        $this->control->accept = implode(', ', Forms\Helpers::getSupportedImages());
1✔
138

139
                } elseif ($validator === Form::MimeType) {
1✔
140
                        $this->control->accept = implode(', ', (array) $arg);
1✔
141

142
                } elseif ($validator === Form::MaxFileSize) {
1✔
143
                        if ($arg > ($ini = Forms\Helpers::iniGetSize('upload_max_filesize'))) {
1✔
144
                                trigger_error("Value of MaxFileSize ($arg) is greater than value of directive upload_max_filesize ($ini).", E_USER_WARNING);
×
145
                        }
146
                        $this->getRules()->removeRule($validator);
1✔
147

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

155
                return parent::addRule($validator, $errorMessage, $arg);
1✔
156
        }
157
}
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