Skip to content

Commit 54184c6

Browse files
committed
rule Image detects supported images
1 parent 8a47f3a commit 54184c6

File tree

6 files changed

+23
-5
lines changed

6 files changed

+23
-5
lines changed

src/Forms/Controls/UploadControl.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public function isOk(): bool
105105
public function addRule($validator, $errorMessage = null, $arg = null)
106106
{
107107
if ($validator === Form::Image) {
108-
$this->control->accept = implode(', ', FileUpload::IMAGE_MIME_TYPES);
108+
$this->control->accept = implode(', ', Forms\Helpers::getSupportedImages());
109109

110110
} elseif ($validator === Form::MimeType) {
111111
$this->control->accept = implode(', ', (array) $arg);

src/Forms/Helpers.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,4 +293,18 @@ public static function getSingleType($reflection): ?string
293293
);
294294
}
295295
}
296+
297+
298+
/** @internal */
299+
public static function getSupportedImages(): array
300+
{
301+
$flag = imagetypes();
302+
return array_filter([
303+
$flag & IMG_GIF ? 'image/gif' : null,
304+
$flag & IMG_JPG ? 'image/jpeg' : null,
305+
$flag & IMG_PNG ? 'image/png' : null,
306+
$flag & IMG_WEBP ? 'image/webp' : null,
307+
$flag & 256 ? 'image/avif' : null, // IMG_AVIF
308+
]);
309+
}
296310
}

src/Forms/Rules.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ public function addRule($validator, $errorMessage = null, $arg = null)
8888
$rule = new Rule;
8989
$rule->control = $this->control;
9090
$rule->validator = $validator;
91-
$this->adjustOperation($rule);
9291
$rule->arg = $arg;
9392
$rule->message = $errorMessage;
93+
$this->adjustOperation($rule);
9494
if ($rule->validator === Form::Filled) {
9595
$this->required = $rule;
9696
} else {
@@ -339,6 +339,10 @@ private function adjustOperation(Rule $rule): void
339339
}
340340
}
341341

342+
if ($rule->validator === Form::Image) {
343+
$rule->arg = Helpers::getSupportedImages();
344+
}
345+
342346
if (!is_callable($this->getCallback($rule))) {
343347
$validator = is_scalar($rule->validator)
344348
? " '$rule->validator'"

src/assets/netteForms.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@
548548
},
549549

550550
image: function (elem, arg, val) {
551-
return Nette.validators.mimeType(elem, ['image/gif', 'image/png', 'image/jpeg', 'image/webp'], val);
551+
return Nette.validators.mimeType(elem, arg || ['image/gif', 'image/png', 'image/jpeg', 'image/webp'], val);
552552
},
553553

554554
'static': function (elem, arg) {

tests/Forms/Controls.UploadControl.render.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ test('accepted files', function () {
7171
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());
7272

7373
$input = $form->addUpload('file3')->addRule(Form::Image);
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());
74+
Assert::match('<input type="file" name="file3" accept="image/gif, image/jpeg, image/png, image/webp%a?%" 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.","arg":["image/gif","image/jpeg","image/png","image/webp"%a?%]}]\'>', (string) $input->getControl());
7575
});
7676

7777

tests/Forms/Forms.renderer.1.expect

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
<tr class="required">
8282
<th><label for="frm-password" class="required">Choose password:</label></th>
8383

84-
<td><input type="password" name="password" id="frm-password" required data-nette-rules='[{"op":":filled","msg":"Choose your password"},{"op":":minLength","msg":"The password is too short: it must be at least 3 characters","arg":3}]' class="text"><input type="password" name="password2" id="frm-password2" data-nette-rules='[{"op":":valid","rules":[{"op":":filled","msg":"Reenter your password"},{"op":":equal","msg":"Passwords do not match","arg":{"control":"password"}}],"control":"password"}]' class="text"><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">
84+
<td><input type="password" name="password" id="frm-password" required data-nette-rules='[{"op":":filled","msg":"Choose your password"},{"op":":minLength","msg":"The password is too short: it must be at least 3 characters","arg":3}]' class="text"><input type="password" name="password2" id="frm-password2" data-nette-rules='[{"op":":valid","rules":[{"op":":filled","msg":"Reenter your password"},{"op":":equal","msg":"Passwords do not match","arg":{"control":"password"}}],"control":"password"}]' class="text"><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","arg":["image/gif","image/jpeg","image/png","image/webp"%a?%]}],"control":"avatar"}]' class="text">
8585
<span class="error">Reenter your password</span></td>
8686
</tr>
8787

0 commit comments

Comments
 (0)