Calling method in custom field type

I'm trying to create a new custom form field and I'm wondering if there's a way to call a method from the component class. For example, if I have the following form field
<x-dynamic-component
:component="$getFieldWrapperView()"
:field="$field"
>
<div x-data="{ state: $wire.$entangle('{{ $getStatePath() }}') }">
<button wire:click="doSomething">Click me!</button>
</div>
</x-dynamic-component>
<x-dynamic-component
:component="$getFieldWrapperView()"
:field="$field"
>
<div x-data="{ state: $wire.$entangle('{{ $getStatePath() }}') }">
<button wire:click="doSomething">Click me!</button>
</div>
</x-dynamic-component>
and the class
class MyCustomComponent extends Field
{
public function doSomething() {
// Nothing useful
}
}
class MyCustomComponent extends Field
{
public function doSomething() {
// Nothing useful
}
}
I get the following error Unable to call component method. Public method [doSomething] not found on component The method obviously exists, but I think it's because the dynamic component doesn't really know where to look. Ss there a way to call the custom doSomething() function in a dynamic component?
Solution:
Form components aren't Livewire components, therefore your method cannot live in the custom field class. You have two options: 1. Add the method to the page (ListRecords, ViewRecord, CreateRecord, EditRecord)...
Jump to solution
4 Replies
epertinez
epertinez16mo ago
+1 I can add that in my case, the component receiving the call is the Widget, not the Column. If I create a dummy doSomething() function in the widget definition class, the error disapears. Yet, it is useless!
Solution
Saade
Saade16mo ago
Form components aren't Livewire components, therefore your method cannot live in the custom field class. You have two options: 1. Add the method to the page (ListRecords, ViewRecord, CreateRecord, EditRecord) 2. Use events instead of methods. See how its done in https://github.com/search?q=repo%3Asaade%2Ffilament-adjacency-list%20builder%3A%3Asort&type=code
Patrick Boivin
Patrick Boivin16mo ago
Linking to a code search like this is super clever! Will surely make use of that in the future 👌
ChesterS
ChesterSOP16mo ago
Yeap, I also asked in the discussions forum and Events seems to be the best approach. Here is the discussion link: https://github.com/filamentphp/filament/discussions/8865
GitHub
[v3][Forms] Call action function without rendering from custom fiel...
I'm trying to create a custom field and I want a way for the field to be able to change some internal state. For example let's say I have the following Component class OptionPicker extends ...

Did you find this page helpful?