Skip to content

Commit 5141f9b

Browse files
committed
UploadControl: appends HTML attribute 'accept' [Closes #172]
1 parent 37553f7 commit 5141f9b

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"require": {
1818
"php": ">=7.1",
1919
"nette/component-model": "^3.0.0-beta2",
20-
"nette/http": "^2.3.8 || ~3.0.0",
20+
"nette/http": "~3.0.0",
2121
"nette/utils": "^3.0-beta2"
2222
},
2323
"require-dev": {

src/Forms/Controls/UploadControl.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,18 @@ public function isOk(): bool
9797
return $carry && $fileUpload->isOk();
9898
}, true);
9999
}
100+
101+
102+
/**
103+
* @return static
104+
*/
105+
public function addRule($validator, $errorMessage = null, $arg = null)
106+
{
107+
if ($validator === Forms\Form::IMAGE) {
108+
$this->control->accept = implode(FileUpload::IMAGE_MIME_TYPES, ', ');
109+
} elseif ($validator === Forms\Form::MIME_TYPE) {
110+
$this->control->accept = implode((array) $arg, ', ');
111+
}
112+
return parent::addRule($validator, $errorMessage, $arg);
113+
}
100114
}

tests/Forms/Controls.UploadControl.render.phpt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,19 @@ test(function () { // validation rules
6363
});
6464

6565

66+
test(function () { // accepted files
67+
$form = new Form;
68+
$input = $form->addUpload('file1')->setRequired(false)->addRule(Form::MIME_TYPE, null, 'image/*');
69+
Assert::same('<input type="file" name="file1" accept="image/*" id="frm-file1" data-nette-rules=\'[{"op":"optional"},{"op":":mimeType","msg":"The uploaded file is not in the expected format.","arg":"image/*"}]\'>', (string) $input->getControl());
70+
71+
$input = $form->addUpload('file2')->setRequired(false)->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":"optional"},{"op":":mimeType","msg":"The uploaded file is not in the expected format.","arg":["image/*","text/html"]}]\'>', (string) $input->getControl());
73+
74+
$input = $form->addUpload('file3')->setRequired(false)->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":"optional"},{"op":":image","msg":"The uploaded file must be image in format JPEG, GIF, PNG or WebP."}]\'>', (string) $input->getControl());
76+
});
77+
78+
6679
test(function () { // container
6780
$form = new Form;
6881
$container = $form->addContainer('container');

0 commit comments

Comments
 (0)