Skip to content

Instantly share code, notes, and snippets.

@einnar82
Created May 13, 2020 01:16
Show Gist options
  • Save einnar82/45df5710a2468fe5d09aee3e45b97d91 to your computer and use it in GitHub Desktop.
Save einnar82/45df5710a2468fe5d09aee3e45b97d91 to your computer and use it in GitHub Desktop.
Excel Validation Rule
<?php
namespace App\Rules\API;
use Illuminate\Contracts\Validation\Rule;
class ExcelRule implements Rule
{
/**
* Create a new rule instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
return $this->checkExcelFile($value->getClientOriginalExtension());
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return 'The file must be a file of type: csv, xlsx, xls';
}
/**
* Check if file is a valid spreadsheet
*/
public function checkExcelFile($file_ext)
{
$valid = [
'csv', 'xls', 'xlsx' // add your extensions here.
];
return in_array($file_ext, $valid) ? true : false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment