Created
May 13, 2020 01:16
-
-
Save einnar82/45df5710a2468fe5d09aee3e45b97d91 to your computer and use it in GitHub Desktop.
Excel Validation Rule
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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