Skip to content

Instantly share code, notes, and snippets.

@sunshain
Last active April 14, 2016 10:50
Show Gist options
  • Save sunshain/8d4f486d24dc6b281b6160f08bb607c7 to your computer and use it in GitHub Desktop.
Save sunshain/8d4f486d24dc6b281b6160f08bb607c7 to your computer and use it in GitHub Desktop.
Laravel custom Validation (mimetypes from url)
<?php
namespace App\Providers;
use Validator;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Validator::extend('mimeurl',
function($attribute, $value, $parameters, $validator) {
$finfo = new \finfo(FILEINFO_MIME_TYPE);
$mimetype = $finfo->buffer(file_get_contents($value));
return in_array($mimetype, $parameters);
}
);
Validator::replacer('mimeurl',
function($message, $attribute, $rule, $parameters) {
return str_replace(':values', implode(', ', $parameters), $message);
}
);
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment