F
Filament12mo ago
Antoine

ColumnSpan : Array to string conversion

Hello everyone, Is it only me who has this problem when I try to make my columns dynamic on any of my fields, I get the following error if I try to return an array from a closure: Array to string conversion Here is my code:
->columnSpan(function($get) {
return $get('show_time') ? [
'default' => 12,
'sm' => 12,
'md' => 3,
'lg' => 3,
'xl' => 3,
'2xl' => 3,
]:[
'default' => 12,
'sm' => 12,
'md' => 9,
'lg' => 9,
'xl' => 9,
'2xl' => 9,
];
})
->columnSpan(function($get) {
return $get('show_time') ? [
'default' => 12,
'sm' => 12,
'md' => 3,
'lg' => 3,
'xl' => 3,
'2xl' => 3,
]:[
'default' => 12,
'sm' => 12,
'md' => 9,
'lg' => 9,
'xl' => 9,
'2xl' => 9,
];
})
Whereas if I pass it an array directly without closure, I have no error. Is this a bug? Or am I the only one getting this problem?
5 Replies
awcodes
awcodes12mo ago
What is $get(‘show_time’) returning? If it’s anything other than true/false/null it will break your ternary.
Antoine
AntoineOP12mo ago
it's a Toggle and it returns true or false. I have the same error with :
->columnSpan(fn() => [
'default' => 12,
'sm' => 12,
'md' => 9,
'lg' => 9,
'xl' => 9,
'2xl' => 9,
]),
->columnSpan(fn() => [
'default' => 12,
'sm' => 12,
'md' => 9,
'lg' => 9,
'xl' => 9,
'2xl' => 9,
]),
and not error with :
->columnSpan([
'default' => 12,
'sm' => 12,
'md' => 9,
'lg' => 9,
'xl' => 9,
'2xl' => 9,
]),
->columnSpan([
'default' => 12,
'sm' => 12,
'md' => 9,
'lg' => 9,
'xl' => 9,
'2xl' => 9,
]),
So it's not the condition but I think it's the Closure...
awcodes
awcodes12mo ago
Sorry. Not sure then. Your code looks correct and the columnSpan method accepts a closure. Is this in a resource or a stand alone form.
Antoine
AntoineOP12mo ago
In a stand alone form, I tested in multi forms and I have the same error. Can you try in your app if this code on any field returns an error?
->columnSpan(fn() => [
'default' => 12,
'sm' => 12,
'md' => 9,
'lg' => 9,
'xl' => 9,
'2xl' => 9,
]),
->columnSpan(fn() => [
'default' => 12,
'sm' => 12,
'md' => 9,
'lg' => 9,
'xl' => 9,
'2xl' => 9,
]),
I suspect a code bug in Filament or my entire application... Thank's for help!
awcodes
awcodes12mo ago
Make sure you are calling $this->form->fill() in the mount() method

Did you find this page helpful?