1

I am currently working on a Filament PHP app and I have a problem: I want to show the username of the creator of a note (in my case) and I want to make the field unmodifiable in Filament. So I have thought of this:

        Select::make('user_id')
                            ->options([
                                User::query()
                                    ->select([
                                        DB::raw("name as name"),
                                        'id',
                                    ])
                                    ->where('id', auth()->id())
                                    ->pluck('name', 'id')
                                    ->toArray()
                            ])
                            ->default(
                                auth()->id()
                            )->label(__('messages.created_by'))
                            ->disabled(),

If i disable the field nothing gets written to the database. I get basically null.

Is there a way to disable the Field and ensure that the user_id gets written into the database?

stevan06
  • 57
  • 4

1 Answers1

1

Disabled HTML inputs will not submit.

The Boolean disabled attribute, when present, makes the element not mutable, focusable, or even submitted with the form.

You can consider using readOnly with default, but it's not available on select, and it's reasonable since if it's a select, how can it be a readOnly field?

You may want to use a text input with default values like below:

Forms\Components\TextInput::make('user_id')
    ->default(
        auth()->id()
    )
    ->readOnly()
    ->label(__('messages.created_by')),

Updated: If you want to show the user's name you can use two inputs as below:

Forms\Components\Hidden::make('user_id')
    ->default(
        auth()->id()
    ),

Forms\Components\TextInput::make('user_name')
    ->default(
        auth()->user()->name
    )
    ->label(__('messages.created_by'))
    ->disabled(),

A hidden user id beside showing the user's name

Update 2: You can use mutateFormDataBeforeCreate in your Create page and customize form data before creating the object. You will need that user's name field from the previous solution, and for the user's id, use the below solution. Here's an example:

...
use Filament\Facades\Filament;


class CreatePost extends CreateRecord
{
    ...
    protected function mutateFormDataBeforeCreate(array $data): array
    {
        $data['user_id'] = Filament::auth()->id();;
        return $data;
    }
}

Saving sensitive data in this manner is more secure.

AmooAti
  • 198
  • 1
  • 10
  • I use select because i want to show the name and use the id as teh value that gets written to the database. – stevan06 Aug 24 '23 at 06:17
  • 1
    @stevan06 I've updated the answer – AmooAti Aug 24 '23 at 06:49
  • thanks! one solution will work. But if you hide or disable the TextInput-> null gets written in te databasel. that is pretty weird. – stevan06 Aug 24 '23 at 07:12
  • @stevan06 Maybe the column (`user_id`) is nullable so that when it's not present in request, database will set it NULL – AmooAti Aug 24 '23 at 07:51
  • yeah sure it is. but the filaemnt does not insert he value of the TextInput into the database if the TextColumn is disabled or hidden. Dont know why – stevan06 Aug 24 '23 at 07:54
  • @stevan06 Ok, I've updated the answer again When a field gets hidden, the input component will not be generated (this must worked but for some reason, it's not generating the ``). we can use `Hidden` component – AmooAti Aug 24 '23 at 08:14
  • @stevan06 I added another solution using hooks. – AmooAti Aug 24 '23 at 08:32
  • hahah yeah i also did this before but the hook is only available if /create is called but for my usecase i have to disable /edit /create etc. and only allow creating though a resource manager. if you click the create button on the resource manager, filament will not redirect you to the /create page, it will create a modal without a route. with this solution the hook does not get called :). but thanks for trying to help. appreciate it – stevan06 Aug 24 '23 at 09:21
  • nice hidden should work. thanks man – stevan06 Aug 24 '23 at 09:24
  • if I change the user. The hidden code you have provided will print the current user. I have a field in my table named user_id which is in realtion with the users tabl. is there any way to show the usernamein the form? – stevan06 Aug 24 '23 at 13:14
  • So, do you want to show the user name in the form? After the hidden code, I placed that for you to show the user's name You can change the field name to anything since it doesn't save to the database. – AmooAti Aug 25 '23 at 07:51