I need to display input types conditionally in both the form and table columns.

If I select 'TextInput' from the 'value_type' dropdown, it will display the TextInput. However, if I choose 'Image,' the FileUpload will appear. By default, both the TextInput and FileUpload are hidden.
public static function form(Form $form): Form
{
return $form
->schema([
Section::make()->schema([
TextInput::make('name'),
Select::make('value_type')
->options([
'TextInput' => 'Text Input',
'FileUpload' => 'Image'
]),

TextInput::make('value'), // If value_type === input
FileUpload::make('value') // If value_type === image
])
]);
}
public static function form(Form $form): Form
{
return $form
->schema([
Section::make()->schema([
TextInput::make('name'),
Select::make('value_type')
->options([
'TextInput' => 'Text Input',
'FileUpload' => 'Image'
]),

TextInput::make('value'), // If value_type === input
FileUpload::make('value') // If value_type === image
])
]);
}
It might not be more challenging if TextColumn and ImageColumn are conditionally displayed based on the value type.
public static function table(Table $table): Table
{
return $table
->columns([
TextColumn::make('name'),
TextColumn::make('value'), // If value_type === input
ImageColumn::make('value'), // If value_type === image
IconColumn::make('status')
)]
}
public static function table(Table $table): Table
{
return $table
->columns([
TextColumn::make('name'),
TextColumn::make('value'), // If value_type === input
ImageColumn::make('value'), // If value_type === image
IconColumn::make('status')
)]
}
3 Replies
Dennis Koch
Dennis Koch14mo ago
Please check the advanced form docs. There is an example for conditional fields.
Abdur Razzaque
Abdur RazzaqueOP14mo ago
@Dennis Koch Thank you very much. I managed to solve the conditional form section by studying the advanced form documentation. However, I haven't been able to find any documentation for the table builder. Could you please provide some guidance on how to display conditional column views?
awcodes
awcodes14mo ago
You cannot conditionally show or hide columns on a table. That’s just not possible with html tables.
Want results from more Discord servers?
Add your server