Skip to content

Instantly share code, notes, and snippets.

@iammart
Forked from nWidart/validation.php
Created November 18, 2019 09:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iammart/96ac93cf26623fd571f0f3cbdcdd3fdd to your computer and use it in GitHub Desktop.
Save iammart/96ac93cf26623fd571f0f3cbdcdd3fdd 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'],
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment