Fetch data from API and fill form

is there anyway to achieve this in filament? a form with a text input, and a fetch button. enter user's social security number, and fetch all other data and fill the form automatically.
Solution:
You can use a suffix action on the text input. Here, you can inject the $state and the $set variables. The latter allows you to set the state values of other fields in your form. I hope this helps ```php return $form ->schema([ Forms\Components\TextInput::make('social_security_number')...
Jump to solution
5 Replies
tuto1902
tuto1902•16mo ago
I'm sure there's a way. I'll do some research and will let you know.
Solution
tuto1902
tuto1902•15mo ago
You can use a suffix action on the text input. Here, you can inject the $state and the $set variables. The latter allows you to set the state values of other fields in your form. I hope this helps
return $form
->schema([
Forms\Components\TextInput::make('social_security_number')
->suffixAction(
Action::make('fetchSocialSecurityInfo')
->icon('heroicon-o-magnifying-glass')
->action(function (Set $set, $state) {
// Check for empty values
if (blank($state)) {
return;
}
// Consume your API
try {
$response = Http::post('https://your-api.com/api/social', ['social_security_number' => $state])->throw()
} catch (Exception) {
Filament::notify('danger', 'Something went wrong');
}
// Set the state of all the other fields
$set('name', $response['name'])
$set('phone', $response['phone'])
// ...
})
)
])
return $form
->schema([
Forms\Components\TextInput::make('social_security_number')
->suffixAction(
Action::make('fetchSocialSecurityInfo')
->icon('heroicon-o-magnifying-glass')
->action(function (Set $set, $state) {
// Check for empty values
if (blank($state)) {
return;
}
// Consume your API
try {
$response = Http::post('https://your-api.com/api/social', ['social_security_number' => $state])->throw()
} catch (Exception) {
Filament::notify('danger', 'Something went wrong');
}
// Set the state of all the other fields
$set('name', $response['name'])
$set('phone', $response['phone'])
// ...
})
)
])
tuto1902
tuto1902•14mo ago
@shaan It's been a few days. Is it OK if I mark this as solved?
shaan
shaanOP•14mo ago
yes. Thank you so much for the help 🙂
Want results from more Discord servers?
Add your server