F
Filament3w ago
o.m

How do I upload file on click only?

public $import_file = null;

protected function getFormSchema(): array
{
return [
Section::make('Import User Instructions')
->description('Instructions for importing users into the system')
->schema([
\Filament\Forms\Components\View::make('partials.import-instructions'),
FileUpload::make('import_file')
->label('Import File')
->acceptedFileTypes(['text/csv', 'application/vnd.ms-excel'])
->helperText('Upload an Excel (.CSV) file with the required user data.'),
])
->compact()
->collapsible()
->footerActions([
Actions\Action::make('importUsers')
->label('Import Users')
->button()
->color('success')
->action(fn() => $this->importUsers()),
])
->footerActionsAlignment(Alignment::End),
];
}

public function importUsers(): void
{
$this->validate([
'import_file' => 'required|file|max:2048',
], [
'import_file.required' => 'Please upload a file.',
]);

Import::start(
file: $this->import_file
);

Notification::make()
->title('File Uploaded')
->success()
->send();
}
public $import_file = null;

protected function getFormSchema(): array
{
return [
Section::make('Import User Instructions')
->description('Instructions for importing users into the system')
->schema([
\Filament\Forms\Components\View::make('partials.import-instructions'),
FileUpload::make('import_file')
->label('Import File')
->acceptedFileTypes(['text/csv', 'application/vnd.ms-excel'])
->helperText('Upload an Excel (.CSV) file with the required user data.'),
])
->compact()
->collapsible()
->footerActions([
Actions\Action::make('importUsers')
->label('Import Users')
->button()
->color('success')
->action(fn() => $this->importUsers()),
])
->footerActionsAlignment(Alignment::End),
];
}

public function importUsers(): void
{
$this->validate([
'import_file' => 'required|file|max:2048',
], [
'import_file.required' => 'Please upload a file.',
]);

Import::start(
file: $this->import_file
);

Notification::make()
->title('File Uploaded')
->success()
->send();
}
I am trying to upload a file however I get this error and do not know what to do next.
No description
7 Replies
Alex-Elivate
Alex-Elivate3w ago
Perhaps try to provide a type hint for your public property. Or set it with a value.
o.m
o.mOP3w ago
Regarding that answr how do I deal with Dynamic fields? Because I am currently using a. dynamic field and I have trouble displaying the values since I have to declare a public property For example this one
foreach ($this->group_settings_sections as $section) {
foreach ($section->settings as $setting) {
$schema[] = \Filament\Forms\Components\Grid::make(2)
->schema([
// Left Column
\Filament\Forms\Components\View::make('partials.label-description')
->viewData([
'title' => $setting->label,
])
->columnSpan(1),

// Right Column
match ($setting->type) {
'point_of_contact' => \Filament\Forms\Components\Group::make()
->schema([
\Filament\Forms\Components\TextInput::make("settings.{$setting->uuid}.name")
->label('Name')
->default(Arr::get(group()->setting($setting->shortcode), 'name')),

\Filament\Forms\Components\TextInput::make("settings.{$setting->uuid}.email")
->label('Email Address')
->default(Arr::get(group()->setting($setting->shortcode), 'email')),
])
->columnSpan(1),
foreach ($this->group_settings_sections as $section) {
foreach ($section->settings as $setting) {
$schema[] = \Filament\Forms\Components\Grid::make(2)
->schema([
// Left Column
\Filament\Forms\Components\View::make('partials.label-description')
->viewData([
'title' => $setting->label,
])
->columnSpan(1),

// Right Column
match ($setting->type) {
'point_of_contact' => \Filament\Forms\Components\Group::make()
->schema([
\Filament\Forms\Components\TextInput::make("settings.{$setting->uuid}.name")
->label('Name')
->default(Arr::get(group()->setting($setting->shortcode), 'name')),

\Filament\Forms\Components\TextInput::make("settings.{$setting->uuid}.email")
->label('Email Address')
->default(Arr::get(group()->setting($setting->shortcode), 'email')),
])
->columnSpan(1),
o.m
o.mOP3w ago
as you see values are not displaying
No description
Alex-Elivate
Alex-Elivate3w ago
I’m not sure how this relates to the original question. Did you try to set the type of the property? For example: public string name. I think your error is from setting the property to type void. If you don’t specify a type in php it has to guess. When you set the value to null php thinks you also want the type to be null.
o.m
o.mOP3w ago
A bit vague, but the dynamic fields looks like this, I am not sure whats the proper way of declaring a property with this one
No description
o.m
o.mOP3w ago
will $public = name[] do ? I really dont know whats the correct structure
Alex-Elivate
Alex-Elivate3w ago
https://www.honeybadger.io/blog/php-type-hinting/. I found an article on type hinting in php. I think understanding how types work will help you understand the error. Your example still makes php guess what the type is. I don’t think this is the answer for fixing everything in your code but it’s an important concept to understand.
Honeybadger Developer Blog
Reducing Errors With Type Hinting in PHP
Errors are unavoidable, but you can always have less of them! In this article, Adebayo Adams walks through how to use type hinting in PHP.

Did you find this page helpful?