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

nette / forms / 10256859813

05 Aug 2024 10:20PM UTC coverage: 93.004% (-0.07%) from 93.07%
10256859813

push

github

dg
added HTML attribute data-nette-error

1 of 1 new or added line in 1 file covered. (100.0%)

28 existing lines in 6 files now uncovered.

2087 of 2244 relevant lines covered (93.0%)

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
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
declare(strict_types=1);
9

10
namespace Nette\Forms\Controls;
11

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

19

20
/**
21
 * Text box and browse button that allow users to select a file to upload to the server.
22
 */
23
class UploadControl extends BaseControl
24
{
25
        /** validation rule */
26
        public const Valid = ':uploadControlValid';
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(...), Forms\Validator::$messages[self::Valid]);
1✔
42
                $this->addRule(Form::MaxFileSize, null, Forms\Helpers::iniGetSize('upload_max_filesize'));
1✔
43
                if ($multiple) {
1✔
44
                        $this->addRule(Form::MaxLength, 'The maximum allowed number of uploaded files is %d', (int) ini_get('max_file_uploads'));
1✔
45
                }
46

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

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

56

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

62

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

68

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

77

78
        public function getValue(): FileUpload|array|null
79
        {
80
                return $this->value ?? ($this->nullable ? null : new FileUpload(null));
1✔
81
        }
82

83

84
        /**
85
         * Has been any file uploaded?
86
         */
87
        public function isFilled(): bool
88
        {
89
                return (bool) $this->value;
1✔
90
        }
91

92

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

102

103
        public function isNullable(): bool
104
        {
UNCOV
105
                return $this->nullable;
×
106
        }
107

108

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

122

123
        public function addRule(
1✔
124
                callable|string $validator,
125
                string|Stringable|null $errorMessage = null,
126
                mixed $arg = null,
127
        ): static
128
        {
129
                if ($validator === Form::Image) {
1✔
130
                        $this->control->accept = implode(', ', Forms\Helpers::getSupportedImages());
1✔
131

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

135
                } elseif ($validator === Form::MaxFileSize) {
1✔
136
                        if ($arg > ($ini = Forms\Helpers::iniGetSize('upload_max_filesize'))) {
1✔
UNCOV
137
                                trigger_error("Value of MaxFileSize ($arg) is greater than value of directive upload_max_filesize ($ini).", E_USER_WARNING);
×
138
                        }
139
                        $this->getRules()->removeRule($validator);
1✔
140

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

148
                return parent::addRule($validator, $errorMessage, $arg);
1✔
149
        }
150
}
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

© 2025 Coveralls, Inc