How to Execute a Function on Button Click and Component Render in Custom Filament Fields?
Hi, I am creating custom fields and want to include a button that, when clicked, executes a function in my component. The function,
sendOtp
, queries the database and calls services, etc. Additionally, I want to call this function when the component is rendered for the first time.
Here is my base code:
Component:
Blade:
How can I make the button execute the sendOtp
function on click, and also call this function when the component is rendered for the first time?Solution:Jump to solution
Hi,
To achieve your goal of creating a custom field with a button that executes the sendOtp function when clicked and also calls this function when the component is rendered for the first time, you can follow these steps:
Component:...
2 Replies
sendOpt method should exist in the pages, in your CreatePage, EditPage... another way would be registering actions in the field
https://filamentphp.com/docs/3.x/forms/actions/#adding-an-action-to-a-custom-form-component
Solution
Hi,
To achieve your goal of creating a custom field with a button that executes the sendOtp function when clicked and also calls this function when the component is rendered for the first time, you can follow these steps:
Component:
First, make sure to set up the action in your AuthField class:
Blade:
Then, update your Blade template to call the sendOtp function when the button is clicked and when the component is initialized:
With these changes, the sendOtp function will be called both when the component is rendered initially and when the button is clicked.