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

I have these 2 fields. The ref_no with formatStateUsing() and ref_no without this method formatStateUsi

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744113648a4559069.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信