Skip to content

Instantly share code, notes, and snippets.

@vluzrmos
Last active January 3, 2017 13:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vluzrmos/4de2cb5a1fcb22b2a60a to your computer and use it in GitHub Desktop.
Save vluzrmos/4de2cb5a1fcb22b2a60a to your computer and use it in GitHub Desktop.
Laravel - Simple file extension validator
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Symfony\Component\HttpFoundation\File\UploadedFile;
class ValidatorServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot() {
$this->app['validator']->extend('ext', function ($attribute, $value, $params) {
if (!($value instanceof UploadedFile && $value->isValid())) {
return false;
}
$ext = $value->getClientOriginalExtension();
return in_array($ext, $params);
});
}
/**
* Register any application services.
*
* @return void
*/
public function register() {
}
}
@vluzrmos
Copy link
Author

vluzrmos commented Jul 1, 2015

Usage

$rules => [
    "field" => 'ext:pdf,docx,png'
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment