Skip to content

Commit

Permalink
Add file macro
Browse files Browse the repository at this point in the history
  • Loading branch information
curder committed Feb 2, 2024
1 parent 4509ac7 commit 2906a49
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions docs/others/macros.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

## 自定义宏

Expand Down Expand Up @@ -372,4 +372,31 @@ Event::micro('logAndDispatch', function($event) {
Form::micro('customInput', function($name, $value) {
return "<input type='text' name='{$name}' value='{$value}' />";
});
```
```

### `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)],
]);

// ...
}
```
:::

0 comments on commit 2906a49

Please sign in to comment.