fillForm on Create Page?

I've created a duplicate action as below:
    Tables\Actions\CreateAction::make('Duplicate')
        ->label('Duplicate')
        ->icon('heroicon-o-document-duplicate')
        ->form(fn (Form $form) => static::form($form->model(static::$model))->columns(2))
        ->fillForm(fn ($record) => $record->toArray()),

Now, while this works alright for a simple resource (pop-up forms), it doesn't fill the form in case of regular resource (individual create / edit pages).

What should I do differently for it to work for regular resource?
Solution
  1. Yea my bad. I was just adopting code from previous messages. You would indeed need to abstract your form fields and reuse them both in your resource and in the replicas form. For example:
public static function form(Form $form): Form
{
    return $form
        ->schema(static::getFormSchemaContent());
}


public static function getFormSchemaContent(): array
{
    return [
        TextInput::make('dummy'),
        Select::make('another_dummy')
    ];
}

Also see: https://filamentphp.com/docs/3.x/panels/resources/creating-records#sharing-fields-between-the-resource-form-and-wizards

Then you could use

ReplicateAction::make()
  ->form(static::getFormSchemaContent())
  ->fillForm(fn ($record) => $record->toArray())
  ->beforeReplicaSaved(function (Model $replica, array $data): void {
      foreach ($replica->getAttributes() as $key => $value) {
          $replica->{$key} = $data[$key] ?? $value;
      }
      $replica->save();
  })


  1. I suppose that is related to 1 because form(fn (Form $form) => static::form($form->model(static::$model))->columns(2)) seems rather unconventional. Not sure where you got that from 🤔
Was this page helpful?