Skip to content

Commit 7b5575f

Browse files
committed
UploadControl: automatically sets MAX_FILE_SIZE [Closes #238]
1 parent ae63cb5 commit 7b5575f

File tree

4 files changed

+12
-10
lines changed

4 files changed

+12
-10
lines changed

src/Forms/Controls/UploadControl.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public function __construct($label = null, bool $multiple = false)
3434
$this->control->multiple = $multiple;
3535
$this->setOption('type', 'file');
3636
$this->addRule([$this, 'isOk'], Forms\Validator::$messages[self::VALID]);
37+
$this->addRule(Form::MAX_FILE_SIZE, null, Forms\Helpers::iniGetSize('upload_max_filesize'));
3738

3839
$this->monitor(Form::class, function (Form $form): void {
3940
if (!$form->isMethod('post')) {
@@ -108,6 +109,8 @@ public function addRule($validator, $errorMessage = null, $arg = null)
108109
$this->control->accept = implode(', ', FileUpload::IMAGE_MIME_TYPES);
109110
} elseif ($validator === Form::MIME_TYPE) {
110111
$this->control->accept = implode(', ', (array) $arg);
112+
} elseif ($validator === Form::MAX_FILE_SIZE) {
113+
$this->getRules()->removeRule($validator);
111114
}
112115
return parent::addRule($validator, $errorMessage, $arg);
113116
}

tests/Forms/Controls.UploadControl.render.phpt

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@ test(function () {
3232
Assert::same('<label for="frm-file">Another label</label>', (string) $input->getLabel('Another label'));
3333

3434
Assert::type(Html::class, $input->getControl());
35-
Assert::same('<input type="file" name="file" id="frm-file">', (string) $input->getControl());
35+
Assert::match('<input type="file" name="file" id="frm-file" data-nette-rules=\'[{"op":":fileSize","msg":"The size of the uploaded file can be up to %d% bytes.","arg":%d%}]\'>', (string) $input->getControl());
3636
});
3737

3838

3939
test(function () { // multiple
4040
$form = new Form;
4141
$input = $form->addMultiUpload('file', 'Label');
4242

43-
Assert::same('<input type="file" name="file[]" multiple id="frm-file">', (string) $input->getControl());
43+
Assert::match('<input type="file" name="file[]" multiple id="frm-file" data-nette-rules=%a%>', (string) $input->getControl());
4444
});
4545

4646

@@ -58,21 +58,20 @@ test(function () { // Html with translator
5858
test(function () { // validation rules
5959
$form = new Form;
6060
$input = $form->addUpload('file')->setRequired('required');
61-
62-
Assert::same('<input type="file" name="file" id="frm-file" required data-nette-rules=\'[{"op":":filled","msg":"required"}]\'>', (string) $input->getControl());
61+
Assert::match('<input type="file" name="file" id="frm-file" required data-nette-rules=\'[{"op":":filled","msg":"required"},{"op":":fileSize",%a%}]\'>', (string) $input->getControl());
6362
});
6463

6564

6665
test(function () { // accepted files
6766
$form = new Form;
6867
$input = $form->addUpload('file1')->addRule(Form::MIME_TYPE, null, 'image/*');
69-
Assert::same('<input type="file" name="file1" accept="image/*" id="frm-file1" data-nette-rules=\'[{"op":":mimeType","msg":"The uploaded file is not in the expected format.","arg":"image/*"}]\'>', (string) $input->getControl());
68+
Assert::match('<input type="file" name="file1" accept="image/*" id="frm-file1" data-nette-rules=\'[{"op":":fileSize",%a%},{"op":":mimeType","msg":"The uploaded file is not in the expected format.","arg":"image/*"}]\'>', (string) $input->getControl());
7069

7170
$input = $form->addUpload('file2')->addRule(Form::MIME_TYPE, null, ['image/*', 'text/html']);
72-
Assert::same('<input type="file" name="file2" accept="image/*, text/html" id="frm-file2" data-nette-rules=\'[{"op":":mimeType","msg":"The uploaded file is not in the expected format.","arg":["image/*","text/html"]}]\'>', (string) $input->getControl());
71+
Assert::match('<input type="file" name="file2" accept="image/*, text/html" id="frm-file2" data-nette-rules=\'[{"op":":fileSize",%a%},{"op":":mimeType","msg":"The uploaded file is not in the expected format.","arg":["image/*","text/html"]}]\'>', (string) $input->getControl());
7372

7473
$input = $form->addUpload('file3')->addRule(Form::IMAGE);
75-
Assert::same('<input type="file" name="file3" accept="image/gif, image/png, image/jpeg, image/webp" id="frm-file3" data-nette-rules=\'[{"op":":image","msg":"The uploaded file must be image in format JPEG, GIF, PNG or WebP."}]\'>', (string) $input->getControl());
74+
Assert::match('<input type="file" name="file3" accept="image/gif, image/png, image/jpeg, image/webp" id="frm-file3" data-nette-rules=\'[{"op":":fileSize",%a%},{"op":":image","msg":"The uploaded file must be image in format JPEG, GIF, PNG or WebP."}]\'>', (string) $input->getControl());
7675
});
7776

7877

@@ -81,7 +80,7 @@ test(function () { // container
8180
$container = $form->addContainer('container');
8281
$input = $container->addUpload('file');
8382

84-
Assert::same('<input type="file" name="container[file]" id="frm-container-file">', (string) $input->getControl());
83+
Assert::match('<input type="file" name="container[file]" id="frm-container-file" data-nette-rules=%a%>', (string) $input->getControl());
8584
});
8685

8786

tests/Forms/Forms.renderer.1.expect

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@
110110
<tr>
111111
<th><label for="frm-avatar">Picture:</label></th>
112112

113-
<td><input type="file" name="avatar" id="frm-avatar" data-nette-rules='[{"op":":filled","rules":[{"op":":image","msg":"Uploaded file is not image"}],"control":"avatar"}]' class="text"></td>
113+
<td><input type="file" name="avatar" id="frm-avatar" data-nette-rules='[{"op":":fileSize","msg":"The size of the uploaded file can be up to %d% bytes.","arg":%d%},{"op":":filled","rules":[{"op":":image","msg":"Uploaded file is not image"}],"control":"avatar"}]' class="text"></td>
114114
</tr>
115115

116116
<tr>

tests/Forms/Forms.renderer.2.expect

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@
9898

9999
<dt><label for="frm-avatar">Picture:</label></dt>
100100

101-
<dd><input type="file" name="avatar" id="frm-avatar" class="text"></dd>
101+
<dd><input type="file" name="avatar" id="frm-avatar" data-nette-rules='[{"op":":fileSize","msg":"The size of the uploaded file can be up to %d% bytes.","arg":%d%}]' class="text"></dd>
102102

103103

104104

0 commit comments

Comments
 (0)