Skip to content

Instantly share code, notes, and snippets.

@voeurnchy
Last active March 5, 2018 08:37
Show Gist options
  • Save voeurnchy/53ae824e6f78a28fc33c5e4d3ad00d3e to your computer and use it in GitHub Desktop.
Save voeurnchy/53ae824e6f78a28fc33c5e4d3ad00d3e to your computer and use it in GitHub Desktop.
validation of image and countrycode
<?php
// add into AppServiceProvider::register()
Validator::extend('imageable', function ($attribute, $value, $params, $validator) {
try {
ImageManagerStatic::make($value);
return true;
} catch (\Exception $e) {
return false;
}
});
Validator::extend('countrycode', function ($attribute, $value, $params, $validator) {
return in_array(strtoupper($value), array_keys(Codes::$countries));
});
//================//
// Usage //
//================//
$this->validate($request, [
'avatar' => 'imageable', // allow base64, all image type, averyting that can be an image.
'country_code' => 'countrycode'
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment