F
Filament3mo ago
Damien

Is it possible to pre-populate this form with some data?

I have a custom widget, with a heading defined like so:
<div class="flex items-center justify-between">
<p class="font-bold text-xl">Contacts</p>
<x-filament::button tag="a" href="{{
\App\Filament\Resources\ContactResource::getUrl(parameters: [
'action' => \Filament\Actions\CreateAction::getDefaultName()
])
}}">
Add Contact
</x-filament::button>
</div>
<div class="flex items-center justify-between">
<p class="font-bold text-xl">Contacts</p>
<x-filament::button tag="a" href="{{
\App\Filament\Resources\ContactResource::getUrl(parameters: [
'action' => \Filament\Actions\CreateAction::getDefaultName()
])
}}">
Add Contact
</x-filament::button>
</div>
This widget lives on a Company details page, what I would like to do when adding the contact, is to be able to pre-populate some of the information that will come from the company, such as the address. Is this possible with my current implementation or would I have to change to work?
19 Replies
toeknee
toeknee3mo ago
<div class="flex items-center justify-between">
<p class="font-bold text-xl">Contacts</p>
<x-filament::button tag="a" href="{{
\App\Filament\Resources\ContactResource::getUrl(parameters: [
'action' => \Filament\Actions\CreateAction::getDefaultName()->mountUsing(fn() => ['name' => 'Tony'])
])
}}">
Add Contact
</x-filament::button>
</div>
<div class="flex items-center justify-between">
<p class="font-bold text-xl">Contacts</p>
<x-filament::button tag="a" href="{{
\App\Filament\Resources\ContactResource::getUrl(parameters: [
'action' => \Filament\Actions\CreateAction::getDefaultName()->mountUsing(fn() => ['name' => 'Tony'])
])
}}">
Add Contact
</x-filament::button>
</div>
Try the above
Damien
Damien3mo ago
I will give that a go, thank you. Sadly, that does not work and gives the following error: Call to a member function mountUsing() on string Likely due to getDefaultName() returning string|null I can change it to this:
<x-filament::button tag="a" href="{{
\App\Filament\Resources\ContactResource::getUrl(parameters: [
'action' => \Filament\Actions\CreateAction::make()->mountUsing(fn () => [
'first_name' => 'Banana'
])->getDefaultName(),
])
}}">
Add Contact
</x-filament::button>
<x-filament::button tag="a" href="{{
\App\Filament\Resources\ContactResource::getUrl(parameters: [
'action' => \Filament\Actions\CreateAction::make()->mountUsing(fn () => [
'first_name' => 'Banana'
])->getDefaultName(),
])
}}">
Add Contact
</x-filament::button>
Which mounts the form again but does not fill the first_name input
toeknee
toeknee3mo ago
Ahh sorry just seen it more so. If you adjust the create page, you should be about to user
public function mount() {

parent::mount();

$this->form->fill(['first_name' => 'Banana']);
public function mount() {

parent::mount();

$this->form->fill(['first_name' => 'Banana']);
Damien
Damien3mo ago
I'll have a look, thank you. Sorry, which create page, for the resource I want to add? It doesn't have a create page as I am using modals, so I have, list,view and edit
toeknee
toeknee3mo ago
So that says the button for add contact goes to a url? Are you making it url button but rending the action within it? Personally, I’ll add the action as per the docs on the livewire component. Then render the action opposed to calling a view button then you can use fill using
Damien
Damien3mo ago
I cannot remember where I got this syntax from, I think I got it from another help thread before as I wanted to navigate and action from a different view. Do you mind linking to the part of the docs you mean? I am unsure where the documentation for this lives if someone is able to help point me in the right direction that would be great.
Damien
Damien3mo ago
Thank you, I will try and move to providing an action this way with the data required. Not sure how I got to the syntax above but I am certain it came from here somewhere 😹 Ah, so I found the original thread where I picked up / discussed the syntax I shared above: https://discord.com/channels/883083792112300104/1204006622750318632 However, I also noted that this is similar to an issue I was having and Dan was helping me with. So moving more inline to what you have suggested, and Dan previously, I now have this code:
<div class="space-y-4 divide-y-2">
<div class="flex items-center justify-between">
<p class="font-bold text-xl">Contacts</p>
{{ $this->createCompanyContactAction }}
</div>

<div class="grid gap-4 grid-cols-1 auto-rows-auto pt-4 divide-y divide-dashed">
@foreach($record->contacts as $contact)
<x-company-resource.contacts-overview.contacts-card-contact :contact="$contact"/>
@endforeach
</div>

<x-filament-actions::modals/>
</div>
<div class="space-y-4 divide-y-2">
<div class="flex items-center justify-between">
<p class="font-bold text-xl">Contacts</p>
{{ $this->createCompanyContactAction }}
</div>

<div class="grid gap-4 grid-cols-1 auto-rows-auto pt-4 divide-y divide-dashed">
@foreach($record->contacts as $contact)
<x-company-resource.contacts-overview.contacts-card-contact :contact="$contact"/>
@endforeach
</div>

<x-filament-actions::modals/>
</div>
and
<?php

namespace App\Filament\Resources\CompanyResource\Widgets;

// ...

class ContactsOverview extends Widget implements HasActions, HasForms
{
use InteractsWithActions;
use InteractsWithForms;

protected static string $view = 'filament.resources.company-resource.widgets.contacts-overview';

public ?Model $record = null;

public function createCompanyContactAction(): Action
{
return Action::make('createCompanyContact')
->label('Add Contact')
->record($this->record)
->form(fn ($form) => ContactResource::form($form))
return Action::make('createCompanyContact')
->label('Add Contact')
->record($this->record)
->form(fn ($form) => ContactResource::form($form))
->action(function () {
dd('Create Company Contact Action');
});
}
}
<?php

namespace App\Filament\Resources\CompanyResource\Widgets;

// ...

class ContactsOverview extends Widget implements HasActions, HasForms
{
use InteractsWithActions;
use InteractsWithForms;

protected static string $view = 'filament.resources.company-resource.widgets.contacts-overview';

public ?Model $record = null;

public function createCompanyContactAction(): Action
{
return Action::make('createCompanyContact')
->label('Add Contact')
->record($this->record)
->form(fn ($form) => ContactResource::form($form))
return Action::make('createCompanyContact')
->label('Add Contact')
->record($this->record)
->form(fn ($form) => ContactResource::form($form))
->action(function () {
dd('Create Company Contact Action');
});
}
}
However I am getting an error around relationships: Here is the error:
Filament\Support\Services\RelationshipJoiner::prepareQueryForNoConstraints(): Argument #1 ($relationship) must be of type Illuminate\Database\Eloquent\Relations\Relation, null given, called in /var/www/html/vendor/filament/forms/src/Components/Select.php on line 773
Filament\Support\Services\RelationshipJoiner::prepareQueryForNoConstraints(): Argument #1 ($relationship) must be of type Illuminate\Database\Eloquent\Relations\Relation, null given, called in /var/www/html/vendor/filament/forms/src/Components/Select.php on line 773
So it is complaining about null being received but I am unsure where this value comes from. Should I be defining a relationship?
toeknee
toeknee3mo ago
You issue is coming from the form. Can you provide the contract resource form? I suspect you are specifying it in here.
Damien
Damien3mo ago
How do you mean sorry? The public method in my ContactResource?
toeknee
toeknee3mo ago
->form(fn ($form) => ContactResource::form($form)) you are using the form, so it's about where it is coming from. we need the form that renders
Damien
Damien3mo ago
I think I follow. $form is a required param so I needs to get the form that the ContactResource uses. The syntax came from Dan in another thread but I guess I can just build a form in there?
toeknee
toeknee3mo ago
Try building the basic's of the form as a test
Damien
Damien3mo ago
I am having a really hard time getting anything to work at the moment. Any new Form I try and create requires livewire I've come back to it after a bit of time off and just struggling to get my brain into gear.
toeknee
toeknee3mo ago
Action::make()->form() requires an array schema? so: ->form([ TextInput::make('name') ])
Damien
Damien3mo ago
gotcha, sorry, I did that previously but it didn't show, I think I must have been missing something else. that is however now rendering a form.
toeknee
toeknee3mo ago
And is it filled?
Damien
Damien3mo ago
adding this to the action:
->fillForm([
'name' => 'John Doe',
])
->fillForm([
'name' => 'John Doe',
])
Does indeed fill the the name field. So I just have to change it to the fields I want to populate and we should be good! Thank you so much!
toeknee
toeknee3mo ago
Welcomes
Want results from more Discord servers?
Add your server
More Posts