Skip to content

Instantly share code, notes, and snippets.

@nWidart
Last active May 31, 2020 13:42
Show Gist options
  • Save nWidart/09fdb6849c189b2c0b86 to your computer and use it in GitHub Desktop.
Save nWidart/09fdb6849c189b2c0b86 to your computer and use it in GitHub Desktop.
A extensions validation rule for Laravel 5
<?php
// For example in AppServiceProvider
Validator::extend('extensions', function ($attribute, $value, $parameters) {
return in_array($value->getClientOriginalExtension(), $parameters);
});
Validator::replacer('extensions', function($message, $attribute, $rule, $parameters) {
return str_replace([':attribute', ':values'], [$attribute, implode(',', $parameters)], $message);
});
// Usage
Rule: extensions:extension1,extension2
In context: 'file' => ['required', 'extensions:extension1,extension2'],
@caztillo
Copy link

Thank you!

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