new Upload("{$uploadFieldId}UploadButtonDiv", "{$uploadFieldId}uploadFileList", {
internalId: '{$uploadField->getInternalId()}',
{if $uploadField->getMaxFiles()}maxFiles: {$uploadField->getMaxFiles()},{/if}
- imagePreview: {if !$uploadField->supportMultipleFiles() && $uploadField->isImageOnly()}true{else}false{/if}
+ imagePreview: {if !$uploadField->supportMultipleFiles() && $uploadField->isImageOnly()}true{else}false{/if},
+ {if $uploadField->getAcceptableFiles()}
+ acceptableFiles: [
+ {implode from=$uploadField->getAcceptableFiles() item=accept}'{$accept|encodeJS}'{/implode}
+ ],
+ {/if}
});
});
-</script>
\ No newline at end of file
+</script>
new Upload("{$uploadFieldId}UploadButtonDiv", "{$uploadFieldId}uploadFileList", {
internalId: '{$uploadField->getInternalId()}',
{if $uploadField->getMaxFiles()}maxFiles: {$uploadField->getMaxFiles()},{/if}
- imagePreview: {if !$uploadField->supportMultipleFiles() && $uploadField->isImageOnly()}true{else}false{/if}
+ imagePreview: {if !$uploadField->supportMultipleFiles() && $uploadField->isImageOnly()}true{else}false{/if},
+ {if $uploadField->getAcceptableFiles()}
+ acceptableFiles: [
+ {implode from=$uploadField->getAcceptableFiles() item=accept}'{$accept|encodeJS}'{/implode}
+ ],
+ {/if}
});
});
-</script>
\ No newline at end of file
+</script>
*/
public $allowSvgImage = false;
+ /**
+ * Acceptable file types.
+ * @var string[]|null
+ * @since 5.3
+ */
+ public $acceptableFiles = null;
+
/**
* UploadField constructor.
*
* Sets the flag for `imageOnly`. This flag indicates whether only images
* can uploaded via this field. Other file types will be rejected during upload.
*
+ * If set to `true` will also set the acceptable types to `image/*`. If set to
+ * false it will clear the acceptable types if they are `image/*`.
+ *
* @param boolean $imageOnly
*/
public function setImageOnly($imageOnly) {
$this->imageOnly = $imageOnly;
+
+ if ($imageOnly) {
+ $this->setAcceptableFiles(['image/*']);
+ }
+ else {
+ // Using == here is safe, because we match a single element array containing
+ // a scalar value.
+ if ($this->getAcceptableFiles() == ['image/*']) {
+ $this->setAcceptableFiles(null);
+ }
+ }
}
/**
$this->allowSvgImage = $allowSvgImage;
}
+
+ /**
+ * Specifies acceptable file types. Use null to not specify any restrictions.
+ *
+ * <strong>Heads up:</strong> This feature is used to improve user experience, by removing
+ * unacceptable files from the file picker. It does not validate the type of the uploaded
+ * file. You are responsible to perform (proper) validation on the server side.
+ *
+ * Valid values are specified as "Unique file type specifiers":
+ * - A case insensitive file extension starting with a dot.
+ * - A mime type.
+ * - `audio/*`
+ * - `image/*`
+ * - `video/*`
+ *
+ * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file#Unique_file_type_specifiers
+ * @param string[]|null $acceptableFiles
+ * @since 5.3
+ */
+ public function setAcceptableFiles($acceptableFiles = null) {
+ $this->acceptableFiles = $acceptableFiles;
+ }
+
+ /**
+ * Returns the acceptable file types.
+ *
+ * @return string[]|null
+ * @since 5.3
+ */
+ public function getAcceptableFiles() {
+ return $this->acceptableFiles;
+ }
}