Use Action inside a custom field
Hello, i have a error in my custom field, because i have 2 form submit buttons.
Field:
forms.components.custom-field.blade.php:
EditCustomer:
The error:
The action is shown correctly, also the modal is asking for confirmation.
But on confimation the save Button in the backgound on the create page is triggered. And the record is saved. But the modal is still open.
Is there a way to separate the "confimation form" on the edit page?
37 Replies
{{ $this->billCreatedAction }}
No () on the action.
Thanks i changed it, but it made no difference with my problem
Here a screenshot from this error.
I hit submit on the modal ("Bestätigen")
You can see the submit button in the background spinning, but the modal is not submiting.
Any updates on this?
technically you should use form component actions for this use case, not normal livewire actions
i dont see the error screenshot either
Hi @Dan Harrin, it does not appear to be an option version 2 of filament.
it is, just not documented
One second, let me have a look.
i think the v3 docs are mostly accurate for v2
Can it be added here (see bottom)?:
public static function form(Form $form): Form
{
return $form
->schema([
Toggle::make('generate_pdf_files')->label('Generate PDF file(s)')->reactive()->default(true),
Forms\Components\Select::make('filing_type')
->label('Filing Type')
->options([
'Monthly' => 'Monthly',
'Quarterly' => 'Quarterly',
])
->default('Quarterly')
->helperText('Select monthly to round by month instead of quarter')
->required(condition: fn(Closure $get): bool => $get('generate_pdf_files'))
->hidden(function (callable $get) {
return !$get('generate_pdf_files');
}),
Forms\Components\Select::make('country_code')
->label('Country')
->options(array("" => "Select Country") + app(GeneralSettings::class)->report_countries)
->default('')
->disablePlaceholderSelection()
->required(condition: fn(Closure $get): bool => $get('generate_pdf_files'))
->hidden(function (callable $get) {
return !$get('generate_pdf_files');
}),
FileUpload::make('filename')
->acceptedFileTypes(['text/csv', 'text/plain'])
->preserveFilenames()
->multiple()
->visibility('private')
->label('CSV/TXT File')
->required()
// <------------------------------------------------ Can it be added here?
])->columns(1);
}
uhhh what are you trying to do
where will the button be in the file upload
This is the idea:
1. User fills form.
2. Once the file is available, have a custom button at the bottom to run an algorithm against the data and download the result into .csv
3. Once create is clicked, it will do what it does now. It runs an alternative algorithm and generates a PDF.
That extra button is there to simply to generate an optional downloadable report.
Alternative solution is probably do whatever it does here.. but after the creation, prompt a popup (Would you like to download a report)...
add a View::make() component
in the view file,
<x-filament::button wire:click="download">Download</x-filament::button>
that is the simplest wayWill give that a try now 🙂
Ah did not work:
Property [$data] not found on component: [app.filament.resources.data-resource.pages.manage-data]
Let me provide you some code...DataResource file
ManageRecords, where the download is:
<?php
namespace App\Filament\Resources\DataResource\Pages;
use App\Filament\Resources\DataResource;
use App\Helpers\Helpers;
use Filament\Pages\Actions\CreateAction;
use Filament\Resources\Pages\ManageRecords;
use Illuminate\Database\Eloquent\Collection;
use Filament\Forms\Components\Actions\Action;
class ManageData extends ManageRecords
{
protected static string $resource = DataResource::class;
protected function getActions(): array
{
$records = new Collection();
return [
CreateAction::make()
->disableCreateAnother()
->using(function (array $data) use (&$records) {
foreach ($data['filename'] as $filename) {
$records[] = static::getModel()::create(['filename' => $filename]);
}
})
->after(function (array $data) use ($records): ?\Symfony\Component\HttpFoundation\StreamedResponse {
if (isset ($data['generate_pdf_files']) && $data['generate_pdf_files'] === true) {
$response = Helpers::generatePDF($data, $records);
return $response;
}
return null;
})
];
}
public function download()
{
// get data here
dd($this->data);
}
protected function getTableRecordsPerPageSelectOptions(): array
{
return [50, 100];
}
}
FormAction button:
<?php
namespace App\Forms\Components;
use Filament\Forms\Components\Field;
class FormActionButton extends Field
{
protected string $view = 'forms.components.form-action-button';
}
And here is the form-action-button.blade.php:
<x-dynamic-component :component="$getFieldWrapperView()" :id="$getId()" :label="$getLabel()" :label-sr-only="$isLabelHidden()" :helper-text="$getHelperText()"
:hint="$getHint()" :hint-action="$getHintAction()" :hint-color="$getHintColor()" :hint-icon="$getHintIcon()" :required="$isRequired()" :state-path="$getStatePath()">
<div x-data="{ state: $wire.entangle('{{ $getStatePath() }}').defer }">
<x-filament::button wire:click="download">Download ESL</x-filament::button>
</div>
</x-dynamic-component>
I tried adding the download function into: ManageRecords
page, but it does not find the component of $data
oh
this appears to work:
public function download()
{
// get data here
dd($this->mountedActionData);
}
if you upgrade to v3 its easier
Thanks Dan 🙂
Yes have it planned in works to upgrade to v3, I'll give that a try tomorrow.
Definetly needing to upgrade now than later
btw, this doesnt need to be a "field"
just a "layout component"
you dont need anything in the view apart from the button
How do you mean?
your view is overcomplicated
Ah that was the default one
I just added the button lol
you dont even need the extra class
just use this and create a file with ONLY the button
Everything else was made by filament when creating the form field using CMD
no other divs or the dynamic component
its not a field. fields have actual state (data) associated
this is just a button
right I see
a field would be an input or something that can store data
Gotcha, yeah spent 5 hours faffing about haha
and replying to every remotely related forum thread instead of just creating a new one 😉
Yeah I did both 🤣