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
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'])
            // ...
          })
      )
  ])
Was this page helpful?