Create 'extensions' custom validator

FileUpload can now specify mime types
This commit is contained in:
Samuel Georges 2015-06-20 10:12:14 +10:00
parent e71025de09
commit 50e4840216
5 changed files with 42 additions and 2 deletions

View File

@ -1,3 +1,6 @@
* **Build 271** (2015-06-20)
- File Upload form widget can now specify `mimeTypes` to define MIME types that are accepted by the uploader, either as file extension or fully qualified name.
* **Build 270** (2015-06-18)
- Introduced the October Storm client-side library.
- Introduced new *MediaFinder* form widget.

View File

@ -56,6 +56,11 @@ class FileUpload extends FormWidgetBase
*/
public $fileTypes = false;
/**
* @var mixed Collection of acceptable mime types.
*/
public $mimeTypes = false;
/**
* @var array Options used for generating thumbnails.
*/
@ -89,6 +94,7 @@ class FileUpload extends FormWidgetBase
'imageHeight',
'previewNoFilesMessage',
'fileTypes',
'mimeTypes',
'thumbOptions',
'useCaption'
]);
@ -347,7 +353,11 @@ class FileUpload extends FormWidgetBase
$validationRules = ['max:'.File::getMaxFilesize()];
if ($fileTypes = $this->getAcceptedFileTypes()) {
$validationRules[] = 'mimes:'.$fileTypes;
$validationRules[] = 'extensions:'.$fileTypes;
}
if ($this->mimeTypes) {
$validationRules[] = 'mimes:'.$this->mimeTypes;
}
$validation = Validator::make(

View File

@ -112,7 +112,7 @@
FileUpload.prototype.onUploadAddedFile = function(file) {
// Remove any exisiting objects for single variety
if (!this.options.isMulti) {
$('> *', this.$filesContainer).not(file.previewElement).remove()
$(file.previewElement).siblings().remove()
}
this.evalIsPopulated()
@ -254,6 +254,11 @@
errorMsg = $('[data-dz-errormessage]', $target).text(),
$template = $(this.options.errorTemplate)
// Remove any exisiting objects for single variety
if (!this.options.isMulti) {
$target.siblings().remove()
}
$target.ocPopover({
content: Mustache.render($template.html(), { errorMsg: errorMsg }),
modal: true,

View File

@ -7,6 +7,7 @@ use Config;
use Backend;
use Request;
use DbDongle;
use Validator;
use BackendMenu;
use BackendAuth;
use Twig_Environment;
@ -52,6 +53,7 @@ class ServiceProvider extends ModuleServiceProvider
$this->registerMailer();
$this->registerMarkupTags();
$this->registerAssetBundles();
$this->registerValidator();
/*
* Register other module providers
@ -447,4 +449,23 @@ class ServiceProvider extends ModuleServiceProvider
});
}
/**
* Extends the validator with custom rules
*/
protected function registerValidator()
{
/*
* Allowed file extensions, as opposed to mime types.
* - extensions: png,jpg,txt
*/
Validator::extend('extensions', function($attribute, $value, $parameters) {
$extension = $value->getClientOriginalExtension();
return in_array($extension, $parameters);
});
Validator::replacer('extensions', function($message, $attribute, $rule, $parameters) {
return strtr($message, [':values' => implode(', ', $parameters)]);
});
}
}

View File

@ -46,6 +46,7 @@ return array(
"array" => "The :attribute may not have more than :max items.",
),
"mimes" => "The :attribute must be a file of type: :values.",
"extensions" => "The :attribute must have an extension of: :values.",
"min" => array(
"numeric" => "The :attribute must be at least :min.",
"file" => "The :attribute must be at least :min kilobytes.",