From 2906a49806003e82ff2f3ce509501d38cc97692e Mon Sep 17 00:00:00 2001 From: curder Date: Fri, 2 Feb 2024 17:26:30 +0800 Subject: [PATCH] Add file macro --- docs/others/macros.md | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/docs/others/macros.md b/docs/others/macros.md index e81b3ec34..290fee6bf 100644 --- a/docs/others/macros.md +++ b/docs/others/macros.md @@ -52,7 +52,7 @@ Laravel 从 4.2 版本开始就有了宏的概念,本文将展示如何创建 - [`Illuminate\Validation\Rule`](https://github.com/laravel/framework/blob/master/src/Illuminate/Validation/Rule.php) - [`Illuminate\View\Factory`](https://github.com/laravel/framework/blob/master/src/Illuminate/View/Factory.php) - [`Illuminate\View\View`](https://github.com/laravel/framework/blob/master/src/Illuminate/View/View.php) - +- [`Illuminate\Validation\Rules\File`](https://github.com/laravel/framework/blob/master/src/Illuminate/Validation/Rules/File.php) ## 自定义宏 @@ -372,4 +372,31 @@ Event::micro('logAndDispatch', function($event) { Form::micro('customInput', function($name, $value) { return ""; }); -``` \ No newline at end of file +``` + +### `File` + +通过 `File` 提供的宏,可以方便的自定义文件类型验证规则。 + +::: code-group +```php [定义] +// AppServiceProvider.php +use Illuminate\Validation\Rules\File; + +File::macro('document', fn() => File::types(['pdf', 'rtf', 'doc', 'docx'])); +``` + +```php [使用] +// UploadController.php +use Illuminate\Validation\Rules\File; + +public function store($request) +{ + $request->validate([ + 'file' => [File::document()->max(20 * 1024)], + ]); + + // ... +} +``` +::: \ No newline at end of file