最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

laravel - filament formatStateUsing() affects other TextInput field - Stack Overflow

programmeradmin13浏览0评论

I have these 2 fields. The ref_no with formatStateUsing() and ref_no without this method formatStateUsing(). Now the method formatStateUsing() affects both ref_no.

How do I prevent this?

TextInput::make('ref_no')
        ->label('Reference No.')
        ->reactive()
        ->disabled()
        ->formatStateUsing(fn ($state) => Str::before($state, '-'))
        ->columnSpan(1),

TextInput::make('ref_no')
        ->label('Reference No.')
        ->columnSpan(2),

I solved the problem by having this. First i added afterStateHydrated() then add a prefix then set the step_two_ref_no to the unmodified value of ref_no

TextInput::make('ref_no')
                ->label('Reference No.')
                ->reactive()
                ->disabled()
                ->afterStateHydrated(function ($component, $state, $set) {
                    $prefix = Str::before($state, '-');
                    $component->state($prefix);
                    $set('step_two_ref_no', $state); 
                })
                ->columnSpan(1),

 TextInput::make('step_two_ref_no')
                    ->label('Reference No.')
                    ->columnSpan(2),

I have these 2 fields. The ref_no with formatStateUsing() and ref_no without this method formatStateUsing(). Now the method formatStateUsing() affects both ref_no.

How do I prevent this?

TextInput::make('ref_no')
        ->label('Reference No.')
        ->reactive()
        ->disabled()
        ->formatStateUsing(fn ($state) => Str::before($state, '-'))
        ->columnSpan(1),

TextInput::make('ref_no')
        ->label('Reference No.')
        ->columnSpan(2),

I solved the problem by having this. First i added afterStateHydrated() then add a prefix then set the step_two_ref_no to the unmodified value of ref_no

TextInput::make('ref_no')
                ->label('Reference No.')
                ->reactive()
                ->disabled()
                ->afterStateHydrated(function ($component, $state, $set) {
                    $prefix = Str::before($state, '-');
                    $component->state($prefix);
                    $set('step_two_ref_no', $state); 
                })
                ->columnSpan(1),

 TextInput::make('step_two_ref_no')
                    ->label('Reference No.')
                    ->columnSpan(2),
Share Improve this question edited Mar 27 at 7:53 Nelson Castro asked Mar 27 at 3:50 Nelson CastroNelson Castro 234 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

All form fields should have a unique name in FilamentPHP, as stated by the docs (emphasis mine):

Fields may be created using the static make() method, passing its unique name. The name of the field should correspond to a property on your Livewire component. You may use “dot notation” to bind fields to keys in arrays.

I'm not entirely sure about the Filament internals, but I assume Filament/Livewire totally relies on the name of the inputs as the only way to distinguish between those. Hence, the results of the method you call on one of the fields, may also get applied to any other field with the same name.

发布评论

评论列表(0)

  1. 暂无评论