Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Pixelairport's avatar

Fill create form in filament before show it in view

I have a laravel table with an action to create new record. Default you can create a new model. I changed it a bit. You first choose a project. The model is a mailing. So you create a mailing for a project. Then you go to the create form page. I want that the subject is prefilled with the name of the project. I visit for example now the url: https://mytesturl.lndo/admin/communications/create?pid=2

pid is the project id. Im searching, what is the best way to do this. When there is no pid, I want to redirect the user back. When there is one I want to get the values of the project to set subject.

At the moment I have the following code in my CommunicationResource. But I think it would be better to do on CreateCommunication file, but dont now how because mutateFormDataBeforeCreate does not work. Field are still empty.

This is the code in CommunictionResource:

protected static function getFormData()
{
   return ['subject' => 'Hello'];
}

public static function form(Form $form): Form
{
  $data = self::getFormData();

  return $form
    ->schema([

      TextInput::make('subject')
        ->default($data['subject'] ?? null)
        ->required(),

But a redirect is not possible, because Form must be type of return for the method. I think there is a better way, but I cant find anything.

0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

To prefill the subject field in your Filament form with the project name based on the pid query parameter, you can utilize the mount method in your CreateCommunication component. This method allows you to access the request data and set default values for your form fields before the form is rendered.

Here's how you can achieve this:

  1. Modify the CreateCommunication Component:

    In your CreateCommunication component, you can use the mount method to check for the pid parameter, fetch the project details, and set the default value for the subject field.

    use App\Models\Project; // Make sure to import your Project model
    
    public function mount()
    {
        $pid = request()->query('pid');
    
        if (!$pid) {
            // Redirect back if no pid is provided
            return redirect()->back();
        }
    
        // Fetch the project using the pid
        $project = Project::find($pid);
    
        if (!$project) {
            // Handle the case where the project is not found
            return redirect()->back();
        }
    
        // Set the default value for the subject
        $this->form->fill([
            'subject' => $project->name, // Assuming 'name' is the field you want to use
        ]);
    }
    
  2. Ensure Your Form is Configured to Use the Default Value:

    Make sure your form field is set up to use the default value provided:

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                TextInput::make('subject')
                    ->required(),
            ]);
    }
    
  3. Handle Redirects Properly:

    Ensure that your application handles redirects properly when the pid is not present or the project is not found. This is done in the mount method as shown above.

By using the mount method, you can effectively prefill the form fields based on the query parameters and handle any necessary redirects. This approach keeps your form logic clean and ensures that the form is prefilled with the correct data before it is displayed to the user.

Please or to participate in this conversation.