F
Filamentβ€’8mo ago
Itachi

how to call a custom function within the action method

In v2 the following code successfully calls a custom function using the 'action' method:
->action(fn () => $this->fillFormFields())
->action(fn () => $this->fillFormFields())
However, in version 3 this code no longer works. Could you please guide me on how to call a custom function within the action method in filament v3?
Solution:
I think in this case the CreateAction is a redirect to the create page. You could use a custom action ```php \Filament\Actions\Action::make('generate') ->action('fillFormFields')...
Jump to solution
7 Replies
toeknee
toekneeβ€’8mo ago
Is fullFormFiles custom? It should work, id you dd within the filleFormFields? you can always try self::fillFormFields but it shouldn't matter as it's just a closure
Itachi
Itachiβ€’8mo ago
yes sir it is custom yes sir i alredy try that .but it donot enter in filleformfields function.
protected function getHeaderActions(): array
{
$formFilled=false;
if($this->data['difficulty_level'] && $this->data['type']){$formFilled= true;
}
return [
Actions\CreateAction::make()
->label('Generate')
->action(fn() => $this->fillFormFields())
->hidden(!$formFilled)
->color($formFilled ? 'success':'danger'),
];
}
protected function fillFormFields()
{
//dd("hii");
$lesson = \App\Models\Lesson::findOrFail($this->data['lesson_id']);
$topic = \App\Models\Topic::find($lesson->topic_id);
$difficulty_level = $this->data['difficulty_level'];
$type = $this->data['type'];
$material = new \App\Models\Material;
$content = json_decode($material->generateContent($topic->title, $lesson->title, $difficulty_level, $type));
if ($this->data['type'] == 'video') {if ($content->view_link) {
$videoId = explode('?v=', $content->view_link)[1];
}}
$data['type'] = $this->data['type'];
$data['difficulty_level'] = $difficulty_level;
$data['lesson_id'] = $this->data['lesson_id'];
$data['embeded_link'] = $content->link;
$data['title'] = $content->title;
$data['duration'] = $content->duration;
if ($this->data['type'] == 'video') {
$data['captions'] = $material->generateCaption($videoId);
}
if ($this->data['type'] == 'text') {
$data['Text'] = $content->Text;
$data['Description'] = $content->Description;
}
if ($this->data['type'] == 'html') {
$data['Html'] = $content->Html;
}
$this->form->fill($data);
}
protected function getHeaderActions(): array
{
$formFilled=false;
if($this->data['difficulty_level'] && $this->data['type']){$formFilled= true;
}
return [
Actions\CreateAction::make()
->label('Generate')
->action(fn() => $this->fillFormFields())
->hidden(!$formFilled)
->color($formFilled ? 'success':'danger'),
];
}
protected function fillFormFields()
{
//dd("hii");
$lesson = \App\Models\Lesson::findOrFail($this->data['lesson_id']);
$topic = \App\Models\Topic::find($lesson->topic_id);
$difficulty_level = $this->data['difficulty_level'];
$type = $this->data['type'];
$material = new \App\Models\Material;
$content = json_decode($material->generateContent($topic->title, $lesson->title, $difficulty_level, $type));
if ($this->data['type'] == 'video') {if ($content->view_link) {
$videoId = explode('?v=', $content->view_link)[1];
}}
$data['type'] = $this->data['type'];
$data['difficulty_level'] = $difficulty_level;
$data['lesson_id'] = $this->data['lesson_id'];
$data['embeded_link'] = $content->link;
$data['title'] = $content->title;
$data['duration'] = $content->duration;
if ($this->data['type'] == 'video') {
$data['captions'] = $material->generateCaption($videoId);
}
if ($this->data['type'] == 'text') {
$data['Text'] = $content->Text;
$data['Description'] = $content->Description;
}
if ($this->data['type'] == 'html') {
$data['Html'] = $content->Html;
}
$this->form->fill($data);
}
toeknee
toekneeβ€’8mo ago
->action(fn() => dd('hit')) Can you check it's being triggered
Itachi
Itachiβ€’8mo ago
no sir,it is not working
Solution
LeandroFerreira
LeandroFerreiraβ€’8mo ago
I think in this case the CreateAction is a redirect to the create page. You could use a custom action
\Filament\Actions\Action::make('generate')
->action('fillFormFields')
\Filament\Actions\Action::make('generate')
->action('fillFormFields')
public function fillFormFields()
{
//...
}
public function fillFormFields()
{
//...
}
Itachi
Itachiβ€’8mo ago
ok sir Thank you sir it works
toeknee
toekneeβ€’8mo ago
That's basically where Im was going πŸ™‚ Good stuff