F
Filament6mo ago
青木

How to dynamically control the configuration of other components in a form?

I need to change another option for selecting components I tried to write it but it doesn't work. Is there any way to achieve this behavior? Also attribute_ids changes I may change another component multilevel dependencies will be updated? For example, a <- b <- c, when c is modified, b needs to be modified at the same time, and a can only be modified when b is done.
Forms\Components\Select::make('attribute_parent_ids')
->label('属性父级')
->options(ProductAttribute::whereNull('parent_id')
->pluck('title', 'id'))
->multiple()
->live(true)
->afterStateUpdated(function (
Forms\Components\Select $component
) {

/** @var Forms\Components\Select $select */
$select = $component->getContainer()
->getComponent('valueIds');

$options = ProductAttribute::whereNotNull('parent_id')
->whereIn('parent_id',
$component->getState())
->pluck('title',
'id');

// remove is cancel options
$newState =
collect($select->getState())
->filter(function ($value) use (
$options
) {
return isset($options[$value]);
})
->toArray();

$select->state($newState)
->options($options);
})
->nullable(),
Forms\Components\Select::make('attribute_ids')
->label('属性')
->options([])
->multiple()
->live(true)
->nullable()
->key('valueIds')
Forms\Components\Select::make('attribute_parent_ids')
->label('属性父级')
->options(ProductAttribute::whereNull('parent_id')
->pluck('title', 'id'))
->multiple()
->live(true)
->afterStateUpdated(function (
Forms\Components\Select $component
) {

/** @var Forms\Components\Select $select */
$select = $component->getContainer()
->getComponent('valueIds');

$options = ProductAttribute::whereNotNull('parent_id')
->whereIn('parent_id',
$component->getState())
->pluck('title',
'id');

// remove is cancel options
$newState =
collect($select->getState())
->filter(function ($value) use (
$options
) {
return isset($options[$value]);
})
->toArray();

$select->state($newState)
->options($options);
})
->nullable(),
Forms\Components\Select::make('attribute_ids')
->label('属性')
->options([])
->multiple()
->live(true)
->nullable()
->key('valueIds')
1 Reply
micraux
micraux6mo ago
There are many examples on the web. Take a look at this part of Filament documentation: https://filamentphp.com/docs/3.x/forms/advanced#dependant-select-options
Want results from more Discord servers?
Add your server