Pass parameter from RelationManager createAction to form

Hi, I'm trying to have a custom create button in my RelationManager in order to create a new record with parameter(s). I've searched and tried several things but still struggle on this. My first idea was to populate an hidden field and use its value in order to perform some custom things, so I tried and used beforeFormFilled() but am unable to populate/retrieve the value. I don't know if I can use an url then access the request(). Can someone help me and point me to the right direction?
Solution:
Ok, I managed to do it: ```php ->beforeFormFilled( function ($livewire) { $livewire->mountedTableActionsData[0]['action_type'] = 'test';...
Jump to solution
13 Replies
PabloZagni
PabloZagni5mo ago
Same here.
PabloZagni
PabloZagni5mo ago
Try this if it helps
No description
PabloZagni
PabloZagni5mo ago
Or this
PabloZagni
PabloZagni5mo ago
No description
Dennis Koch
Dennis Koch5mo ago
It would help if you shared your current code and what exactly you want to do so we understand better.
My first idea was to populate an hidden field
Did you use a Hidden::make() field or a field that's hidden TextInput::make()->hidden()? Last one won't be included in the request.
PabloZagni
PabloZagni5mo ago
Great tip, I didn't know that difference between Hidden::make and Texinput::make()->hidden(), usefull!
Dennis Koch
Dennis Koch5mo ago
One is a type="hidden" and the other one is a field that's hidden/removed from DOM and therefore it's not sent
micraux
micrauxOP5mo ago
Hello @Dennis Koch and @PabloZagni , Thanks for helping, very much appreciated! What I am trying to achieve is, from a createAction on a RelationManager, passing a parameter to the resource form so that I can customize the resource form. My idea was to hide the action_type field because I don't need to save it in the DB. Here is an example of my code: first, I am trying to just see the value "test" in the Action Type field, then I would hide it. I don't know if it is the best way to do it.
// OrdersRelationManager.php
public function table(Table $table): Table
{
return OrderResource::table($table)
->headerActions([
Tables\Actions\createAction::make()
->beforeFormFilled(
function (array $data): array {
$data['action_type'] = 'test';
return $data;
}
)
]);
}

// OrderResource.php
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('action_type'),
...
// OrdersRelationManager.php
public function table(Table $table): Table
{
return OrderResource::table($table)
->headerActions([
Tables\Actions\createAction::make()
->beforeFormFilled(
function (array $data): array {
$data['action_type'] = 'test';
return $data;
}
)
]);
}

// OrderResource.php
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('action_type'),
...
`
Dennis Koch
Dennis Koch5mo ago
My idea was to hide the action_type field because I don't need to save it in the DB.
Yeah, but that also means it's not included in the result. You can try accessing it directly via $livewire->data or removing it before save.
micraux
micrauxOP5mo ago
I get this message: Property [$data] not found on component: [app.filament.application.resources.client-resource.relation-managers.orders-relation-manager]
// OrderResource.php
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('action_type'),
->default(
fn ($livewire) => $livewire->data['action_type']
),
...
// OrderResource.php
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('action_type'),
->default(
fn ($livewire) => $livewire->data['action_type']
),
...
😦
Dennis Koch
Dennis Koch5mo ago
I think it's $formData for RMs Setting the default to the actual value doesn't really make sense though.
->default(
fn ($livewire) => $livewire->data['action_type']
),
->default(
fn ($livewire) => $livewire->data['action_type']
),
micraux
micrauxOP5mo ago
$formData doesn't work either...
Setting the default to the actual value doesn't really make sense though.
Yes, but how can I check if the field is populated during development? @PabloZagni : can you tell me how you can check the value of your $data['type’] (in your second screenshot) in your form?
Solution
micraux
micraux5mo ago
Ok, I managed to do it:
->beforeFormFilled(
function ($livewire) {
$livewire->mountedTableActionsData[0]['action_type'] = 'test';
}
)
->form([
Forms\Components\TextInput::make('whatever')
->default(
fn (Get $get) => $get('action_type')
),
])
->beforeFormFilled(
function ($livewire) {
$livewire->mountedTableActionsData[0]['action_type'] = 'test';
}
)
->form([
Forms\Components\TextInput::make('whatever')
->default(
fn (Get $get) => $get('action_type')
),
])
I don't know if it is the best/simplest way to do it, but that works! Thanks again, guys! 👍
Want results from more Discord servers?
Add your server