F
Filament14mo 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
vasOP14mo 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
awcodes14mo ago
->disabled(fn($context) => $context === ‘edit’) ->hiddenOn(‘create’)
vas
vasOP14mo ago
Undefined constant "App\Filament\Resources‘create’"
awcodes
awcodes14mo ago
Weird. So do the same thing as disabled. ->hidden(fn() => …)
vas
vasOP14mo ago
my ide says Undefined constant '‘create’' same for edit
awcodes
awcodes14mo ago
Show the code.
vas
vasOP14mo 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
vasOP14mo ago
Lightshot
Screenshot
Captured with Lightshot
awcodes
awcodes14mo 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
vasOP14mo 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
awcodes14mo ago
That should be working.
vas
vasOP14mo 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
awcodes14mo ago
Then use that if it’s working but that’s all that context is doing under the hood.
vas
vasOP14mo ago
yea that one is working
awcodes
awcodes14mo ago
So use it.
vas
vasOP14mo ago
thank you again for your time 👍
awcodes
awcodes14mo ago
No worries. Surprised context isn’t working though.
vas
vasOP14mo ago
maybe its something in my setup , does it work in your enviroment ?
awcodes
awcodes14mo ago
vas
vasOP14mo 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
awcodes14mo ago
Yea. Tables use dot syntax to eager load relationships. Forms don’t.
vas
vasOP14mo 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
awcodes14mo ago
You could use a select or any of the layout components. They allow a relationship.
vas
vasOP14mo ago
or is there a better way
awcodes
awcodes14mo ago
All the layout components support relationships.
vas
vasOP14mo ago
when you refer to layout components which ones are those exactly sorry
awcodes
awcodes14mo ago
That’s a start.
vas
vasOP14mo 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
vas14mo 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
awcodes14mo 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
vasOP14mo ago
ok thanks much appreciated seeing things more clearly now 🙂
Want results from more Discord servers?
Add your server