F
Filament2y ago
vas

field disabled on the edit page and hidden on the create new record page

hi i would like to have this field disabled on the edit page and hidden on the create new record page
Forms\Components\TextInput::make('user_id')
->required()
->maxLength(255),
Forms\Components\TextInput::make('user_id')
->required()
->maxLength(255),
any idea how could i accomplish this ? thank you
Solution:
```php return $form ->schema([ Forms\Components\TextInput::make('location') ->required()...
Jump to solution
32 Replies
vas
vasOP2y ago
protected function mutateFormDataBeforeCreate(array $data): array
{
$data['user_id'] = auth()->id();

return $data;
}
protected function mutateFormDataBeforeCreate(array $data): array
{
$data['user_id'] = auth()->id();

return $data;
}
im using this when creating new record i just want to show the user when editing but not be able to change it
awcodes
awcodes2y ago
->disabled(fn($context) => $context === ‘edit’) ->hiddenOn(‘create’)
vas
vasOP2y ago
Undefined constant "App\Filament\Resources‘create’"
awcodes
awcodes2y ago
Weird. So do the same thing as disabled. ->hidden(fn() => …)
vas
vasOP2y ago
my ide says Undefined constant '‘create’' same for edit
awcodes
awcodes2y ago
Show the code.
vas
vasOP2y ago
return $form
->schema([
Forms\Components\TextInput::make('location')
->required()
->maxLength(255),
Forms\Components\TextInput::make('user_id')
->disabled(fn($context) => $context === ‘edit’)
->hiddenOn(‘create’),
Forms\Components\DatePicker::make('date')
->required(),
Forms\Components\TimePicker::make('time')
->required(),
Forms\Components\FileUpload::make('attachments')
->image()
->maxFiles(10)
->multiple()->downloadable(),
Forms\Components\Textarea::make('description')
->required()
->columnSpanFull(),
Forms\Components\Textarea::make('corrective_action')
->columnSpanFull(),
]);
return $form
->schema([
Forms\Components\TextInput::make('location')
->required()
->maxLength(255),
Forms\Components\TextInput::make('user_id')
->disabled(fn($context) => $context === ‘edit’)
->hiddenOn(‘create’),
Forms\Components\DatePicker::make('date')
->required(),
Forms\Components\TimePicker::make('time')
->required(),
Forms\Components\FileUpload::make('attachments')
->image()
->maxFiles(10)
->multiple()->downloadable(),
Forms\Components\Textarea::make('description')
->required()
->columnSpanFull(),
Forms\Components\Textarea::make('corrective_action')
->columnSpanFull(),
]);
vas
vasOP2y ago
Lightshot
Screenshot
Captured with Lightshot
awcodes
awcodes2y ago
Weird. Like I said try the same thing but use hidden instead of hiddenOn. $context is injected into most field modifier callbacks so you should be able to check against it.
vas
vasOP2y ago
Forms\Components\TextInput::make('user_id')
->disabled(fn($context) => $context === ‘edit’)
->hidden(fn($context) => $context === ‘create’),
Forms\Components\TextInput::make('user_id')
->disabled(fn($context) => $context === ‘edit’)
->hidden(fn($context) => $context === ‘create’),
i get the same thing
awcodes
awcodes2y ago
That should be working.
vas
vasOP2y ago
its not this is
->disabled(fn ($livewire) => $livewire instanceof EditRecord),
->disabled(fn ($livewire) => $livewire instanceof EditRecord),
idk how if its correct its generated by copilot
awcodes
awcodes2y ago
Then use that if it’s working but that’s all that context is doing under the hood.
vas
vasOP2y ago
yea that one is working
awcodes
awcodes2y ago
So use it.
vas
vasOP2y ago
thank you again for your time 👍
awcodes
awcodes2y ago
No worries. Surprised context isn’t working though.
vas
vasOP2y ago
maybe its something in my setup , does it work in your enviroment ?
vas
vasOP2y ago
i see , well idk you know more than me 🙂 im just glad it works , thank you if i can bug you a bit if you look at the code there i get the user_id i would like to get the name but using the user.name doesn't return anything
protected $fillable = [
'location',
'date',
'time',
'description',
'created_at',
'attachments',
'user_id',

];


public function user() {
return $this->belongsTo('App\Models\User');
}


protected $casts = [
'attachments' => 'array',
];
protected $fillable = [
'location',
'date',
'time',
'description',
'created_at',
'attachments',
'user_id',

];


public function user() {
return $this->belongsTo('App\Models\User');
}


protected $casts = [
'attachments' => 'array',
];
this is my resource model it works for the table
Tables\Columns\TextColumn::make('user.name')
->searchable(),
Tables\Columns\TextColumn::make('user.name')
->searchable(),
awcodes
awcodes2y ago
Yea. Tables use dot syntax to eager load relationships. Forms don’t.
vas
vasOP2y ago
the only way i found is using select there
Forms\Components\Select::make('user_id')
->options(
fn () => \App\Models\User::all()->pluck('name', 'id')->toArray()
)
Forms\Components\Select::make('user_id')
->options(
fn () => \App\Models\User::all()->pluck('name', 'id')->toArray()
)
awcodes
awcodes2y ago
You could use a select or any of the layout components. They allow a relationship.
vas
vasOP2y ago
or is there a better way
awcodes
awcodes2y ago
All the layout components support relationships.
vas
vasOP2y ago
when you refer to layout components which ones are those exactly sorry
awcodes
awcodes2y ago
That’s a start.
vas
vasOP2y ago
thanks well this works but is this the right way to use a fieldset
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('location')
->required()
->maxLength(255),
Fieldset::make('User Information')
->relationship('user', 'name')
->schema([
Forms\Components\TextInput::make('name')
->disabled(fn ($livewire) => $livewire instanceof EditRecord)
->hidden(fn ($livewire) => $livewire instanceof CreateRecord),
]),
Forms\Components\DatePicker::make('date')
->required(),
Forms\Components\TimePicker::make('time')
->required(),
Forms\Components\FileUpload::make('attachments')
->image()
->maxFiles(10)
->multiple()->downloadable(),
Forms\Components\Textarea::make('description')
->required()
->columnSpanFull(),
Forms\Components\Textarea::make('corrective_action')
->columnSpanFull(),
]);
}
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('location')
->required()
->maxLength(255),
Fieldset::make('User Information')
->relationship('user', 'name')
->schema([
Forms\Components\TextInput::make('name')
->disabled(fn ($livewire) => $livewire instanceof EditRecord)
->hidden(fn ($livewire) => $livewire instanceof CreateRecord),
]),
Forms\Components\DatePicker::make('date')
->required(),
Forms\Components\TimePicker::make('time')
->required(),
Forms\Components\FileUpload::make('attachments')
->image()
->maxFiles(10)
->multiple()->downloadable(),
Forms\Components\Textarea::make('description')
->required()
->columnSpanFull(),
Forms\Components\Textarea::make('corrective_action')
->columnSpanFull(),
]);
}
Solution
vas
vas2y ago
return $form
->schema([
Forms\Components\TextInput::make('location')
->required()
->maxLength(255),
Forms\Components\Fieldset::make('User Information')
->relationship('user', 'name')
->schema([
Forms\Components\TextInput::make('name')
->disabled(fn ($livewire) => $livewire instanceof EditRecord)
])->hidden(fn ($livewire) => $livewire instanceof CreateRecord),
Forms\Components\DatePicker::make('date')
->required(),
Forms\Components\TimePicker::make('time')
->required(),
Forms\Components\FileUpload::make('attachments')
->image()
->maxFiles(10)
->multiple()->downloadable(),
Forms\Components\Textarea::make('description')
->required()
->columnSpanFull(),
Forms\Components\Textarea::make('corrective_action')
->columnSpanFull(),
]);
}
return $form
->schema([
Forms\Components\TextInput::make('location')
->required()
->maxLength(255),
Forms\Components\Fieldset::make('User Information')
->relationship('user', 'name')
->schema([
Forms\Components\TextInput::make('name')
->disabled(fn ($livewire) => $livewire instanceof EditRecord)
])->hidden(fn ($livewire) => $livewire instanceof CreateRecord),
Forms\Components\DatePicker::make('date')
->required(),
Forms\Components\TimePicker::make('time')
->required(),
Forms\Components\FileUpload::make('attachments')
->image()
->maxFiles(10)
->multiple()->downloadable(),
Forms\Components\Textarea::make('description')
->required()
->columnSpanFull(),
Forms\Components\Textarea::make('corrective_action')
->columnSpanFull(),
]);
}
tweaked 😄
awcodes
awcodes2y ago
Totally fine. A layout form component with ->relationship() is the same thing as a table column using the dot syntax. Forms just can’t use the dot syntax for other reasons.
vas
vasOP2y ago
ok thanks much appreciated seeing things more clearly now 🙂

Did you find this page helpful?