armincms/chain

一个 Laravel Nova 字段。

0.4.2 2020-11-13 13:20 UTC

This package is auto-updated.

Last update: 2024-09-13 21:30:55 UTC


README

条件加载具有其他字段值的字段。

安装

通过 composer: composer require armincms/chain

用法

链字段作为一个 触发器监听器 工作。一个 触发器 链监听整个字段的更新事件,而 监听器 链则监听触发器链事件及其整个字段。

创建触发器字段

通过 as 方法初始化 触发器 链。该方法接受两个参数,第一个参数是 事件 的名称,第二个参数是 fields 回调。它接收一个请求实例以解析字段。

        Chain::as('name', function() {
            return [ 
                Text::make('Name')
                    ->sortable()
                    ->rules('required', 'max:255'), 

                Text::make('Username')
                    ->sortable()
                    ->rules('required', 'max:255'), 
            ];  
        }), 

创建监听器字段

通过 with 方法初始化 监听器 链。该方法接受第一个参数为 事件数组,第二个参数为字段回调。要监听一个 触发器 链,应传递链的 名称 作为 事件 名称。要监听一个 触发器 链的字段,可以使用链的名称作为字段名称的前缀并监听它。例如,您可以监听名为 'test' 的链(链字段名为 'test')的 name 字段,使用 test.name 事件。

        Chain::with('name', function($request) {
            if($request->filled('username')){
                return [ 
                    Text::make('Password')
                        ->sortable()
                        ->rules('required', 'max:255'), 
                        
                    Text::make('Confirmation')
                        ->sortable()
                        ->rules('required', 'max:255'), 
                ];  
            }
            
            return [];
        }, 'second-chain'), 

        
        Chain::with('second-chain.confirmation', function($request) {
            return [ 
                Boolean::make('Passed')
                  ->readonly()
                  ->withMeta(['value' => 1]), 
            ]; 
        }),