F
Filamentβ€’9mo ago
Lars

Update hidden fields (Relationship)

So I have a select input that is a relation. This field is hidden or shown based on the type of the user. But when type is changed to Customer the relation, the select, should be updated. I know that the field is not being updated because it is hidden. Is their away around this?
return $form
->schema([
Forms\Components\Group::make()->schema([
Forms\Components\Section::make()->schema([
Forms\Components\TextInput::make('name')
->label('Name')
->required()
->placeholder('John Doe')
->maxLength(255)
->columns(2),
Forms\Components\TextInput::make('email')
->label('Email')
->required()
->columns(2),
Forms\Components\Select::make('type')
->label('Type')
->options(UserRoles::toArray())
->afterStateUpdated(function (Get $get, Set $set) {
if ($get('type') == UserRoles::CUSTOMER->value) {
$set('groups', null);
}

})
->live()
->required(),
Forms\Components\Select::make('groups')
->relationship('groups')
->label('Groups')
->options(Group::all()->pluck('name', 'id'))
->multiple()
->required()
->hidden(fn(Get $get) => $get('type') == UserRoles::CUSTOMER->value),
])->columns(2),
])->columnSpanFull(),
]);

return $form
->schema([
Forms\Components\Group::make()->schema([
Forms\Components\Section::make()->schema([
Forms\Components\TextInput::make('name')
->label('Name')
->required()
->placeholder('John Doe')
->maxLength(255)
->columns(2),
Forms\Components\TextInput::make('email')
->label('Email')
->required()
->columns(2),
Forms\Components\Select::make('type')
->label('Type')
->options(UserRoles::toArray())
->afterStateUpdated(function (Get $get, Set $set) {
if ($get('type') == UserRoles::CUSTOMER->value) {
$set('groups', null);
}

})
->live()
->required(),
Forms\Components\Select::make('groups')
->relationship('groups')
->label('Groups')
->options(Group::all()->pluck('name', 'id'))
->multiple()
->required()
->hidden(fn(Get $get) => $get('type') == UserRoles::CUSTOMER->value),
])->columns(2),
])->columnSpanFull(),
]);

9 Replies
Dennis Koch
Dennis Kochβ€’9mo ago
Hm, I would expect this to work. So the data should save when the select is visible? πŸ€”
Lars
LarsOPβ€’9mo ago
It should always save, regardless of the visibility of the select. Since if the type changes from e.g. admin, teamlead to customer. The record in the pivot should be deleted. Because a customer type cannot be associated with groups.
Dennis Koch
Dennis Kochβ€’9mo ago
Can you try ->dehydrated(true) after the hidden method. Maybe that's already enough.
Lars
LarsOPβ€’9mo ago
Sadly, that doesn't work. After saving the data is still present in the database. Because I changed the type from Teamlead to Customer i expect this table to be empty.
No description
CT
CTβ€’9mo ago
Funny timing, I just had a very similar issue I solved yesterday. Basically hidden or non-visible fields are not updated (even if you manually update the values with for example using $set and/or using ->dehydrated()). I solved my issue by doing this;
->actions([
Tables\Actions\EditAction::make()
->using(function (Project $project, array $data): Model {

// Since filament does not update hidden fields we need to first set these to null
$project->seo_monthly_budget_eur = null;
$project->seo_starts_at = null;
$project->seo_ends_at = null;

// Since filament does not update hidden fields we need to first set these to null
$project->sea_monthly_budget_eur = null;
$project->sea_starts_at = null;
$project->sea_ends_at = null;

// Since filament does not update hidden fields we need to first set these to null
$project->sma_monthly_budget_eur = null;
$project->sma_starts_at = null;
$project->sma_ends_at = null;

// and now we override them with the actual values
$project->update($data);

return $project;
}),
])
->actions([
Tables\Actions\EditAction::make()
->using(function (Project $project, array $data): Model {

// Since filament does not update hidden fields we need to first set these to null
$project->seo_monthly_budget_eur = null;
$project->seo_starts_at = null;
$project->seo_ends_at = null;

// Since filament does not update hidden fields we need to first set these to null
$project->sea_monthly_budget_eur = null;
$project->sea_starts_at = null;
$project->sea_ends_at = null;

// Since filament does not update hidden fields we need to first set these to null
$project->sma_monthly_budget_eur = null;
$project->sma_starts_at = null;
$project->sma_ends_at = null;

// and now we override them with the actual values
$project->update($data);

return $project;
}),
])
I'm happy to submit a PR later to the docs but I'm not even sure if this is expected behaviour or not? Is it supposed to work when using ->dehydrated(true) @Dennis Koch ? Actually, aren't all fields ->dehydrated(true) by default? There is also a ->dehydratedWhenHidden() method which I played with but was unable to get working, same behaviour
Dennis Koch
Dennis Kochβ€’9mo ago
Hm, so I guess that $set() is not working when fields are hidden and not the saving? I am not really sure about this one
CT
CTβ€’9mo ago
$set() itself "works", as in I could change the value to null, then verify and get back the same null using $get() but it simply doesn't save to the database, it would always skip any hidden fields if you get what I mean? so it was pretty confusing for me to figure out what was going on, but I am also only using filament for a couple weeks now so still quite new
Dennis Koch
Dennis Kochβ€’9mo ago
Hm, sounds weird if you are using ->dehydratedWhenHidden(). But I have never used it, so I can't say much about it
Blackpig
Blackpigβ€’9mo ago
I had something similar when I wanted to manipulate the value of hidden field ended removing the hidden field and adding logic to https://filamentphp.com/docs/3.x/panels/resources/editing-records#customizing-data-before-saving
Want results from more Discord servers?
Add your server