yassi / nova-nested-form
一个Laravel Nova包,允许您从父表单创建/更新/删除嵌套相关字段。
Requires
- php: >=7.1.0
Requires (Dev)
- laravel/nova: ^3.0
This package is auto-updated.
Last update: 2024-09-04 15:02:51 UTC
README
Nova Nested Form
此包允许您将嵌套关系的表单包含到父表单中。
安装
composer require yassi/nova-nested-form
贡献
由于我没有预料到会有这么多人使用该包(这很棒),而且我也没有足够的时间经常自行更新/增强此包,因此我正在寻找其他贡献者来帮助我进行维护和功能请求。如果您对此感兴趣,请随时联系我!
更新到3.0
afterFill 和 beforeFill 方法不再可用。
将新关系表单附加到资源
只需将NestedForm添加到您的字段中。第一个参数必须是一个现有的NovaResource类,第二个参数(可选)必须是在您的模型中存在的HasOneOrMany关系。
namespace App\Nova; use Laravel\Nova\Fields\ID; use Illuminate\Http\Request; use Laravel\Nova\Fields\Text; use Laravel\Nova\Fields\Gravatar; use Laravel\Nova\Fields\Password; // Add use statement here. use Yassi\NestedForm\NestedForm; class User extends Resource { ... public function fields(Request $request) { return [ ID::make()->sortable(), Gravatar::make(), Text::make('Name') ->sortable() ->rules('required', 'max:255'), Text::make('Email') ->sortable() ->rules('required', 'email', 'max:254') ->creationRules('unique:users,email') ->updateRules('unique:users,email,{{resourceId}}'), Password::make('Password') ->onlyOnForms() ->creationRules('required', 'string', 'min:6') ->updateRules('nullable', 'string', 'min:6'), // Add NestedForm here. NestedForm::make('Posts'), ]; }
选择何时显示表单
例如,如果嵌套表单仅在"has_comments"属性值为true时才可用,您可以使用
class Post extends Resource { ... public function fields(Request $request) { return [ Boolean::make('Has Comments'), NestedForm::make('Comments')->displayIf(function ($nestedForm, $request) { return [ [ 'attribute' => 'has_comments', 'is' => true ] ]; ]; } })
displayIf 方法预期返回一个数组,因为您可能需要添加多个条件。
class Post extends Resource { ... public function fields(Request $request) { return [ Boolean::make('Has Comments'), Text::make('Title'), Text::make('Subtitle')->nullable(), Number::make('Number of comments allowed'), NestedForm::make('Comments')->displayIf(function ($nestedForm, $request) { return [ [ 'attribute' => 'has_comments', 'is' => true ], [ 'attribute' => 'title', 'isNotNull' => true ], [ 'attribute' => 'subtitle', 'isNull' => true ], [ 'attribute' => 'title', 'includes' => 'My' ], [ 'attribute' => 'number_of_comments_allowed', 'moreThanOrEqual' => 1 ], // Integration for nova booleanGroup field [ 'attribute' => 'my_multiple_checkbox', 'booleanGroup' => 'the_checkbox_key_to_target' ], ]; }) ]; } }
该包将添加这些条件,并根据您填写字段的情况动态更新您的表单。可用的规则有
- 是
- 不是
- 为空
- 不为空
- 大于
- 大于等于
- 小于
- 小于等于
- 包含
- 布尔分组
添加子元素的最小或最大数量
例如,如果您希望每个用户至少有3篇文章,最多有5篇文章,只需使用
NestedForm::make('Posts')->min(3)->max(5),
请注意,该包会自动检测关系是否接受多个子元素或单个子元素,并相应地设置最大值。
当创建新用户时,将显示3个空白文章。如果您达到文章的最大数量,"添加新文章"按钮将消失。
设置默认展开/折叠行为
如果您想默认展开嵌套表单,只需使用
NestedForm::make('Posts')->open(true),
修改默认标题
您可以使用heading()方法修改默认标题。您可以使用helper方法 wrapIndex() 将当前子元素索引添加到您的标题中。
NestedForm::make('Posts')->heading(NestedForm::wrapIndex() . ' // Post'),
您还可以使用helper方法 wrapAttribute() 将当前子元素的任何属性添加到您的标题中。
NestedForm::make('Posts')->heading(NestedForm::wrapIndex() . ' // ' . NestedForm::wrapAttribute('title', 'My default title')),
修改索引分隔符
当您有嵌套表单时(例如,1. 文章,1.1. 评论,1.1.1. 点赞),您可以在具有嵌套表单时使用separator()方法修改默认索引分隔符。
NestedForm::make('Posts')->separator('\'),