F
Filament8mo ago
Finn

Let Filament decrypt database value in TextInput

I got a TextInput field in a form in Filament which is a encrypted value in the database. I have the Laravel cast encrypted on it in my model, but Filament does not seem to decrypt the value if you edit 🤔
16 Replies
Finn
FinnOP8mo ago
This is the TextInput:
TextInput::make('password')
->label('Wachtwoord')
->password()
->revealable()
->required(),
TextInput::make('password')
->label('Wachtwoord')
->password()
->revealable()
->required(),
and in my model:
protected $casts = [
'password' => 'encrypted',
];
protected $casts = [
'password' => 'encrypted',
];
Dennis Koch
Dennis Koch8mo ago
Did you check it works without Filament?
Finn
FinnOP8mo ago
Yes it does decrypt it with php artisan tinker
Dennis Koch
Dennis Koch8mo ago
So if you reveal the password it's still encrypted?
Finn
FinnOP8mo ago
It doesnt fill the value at all in Filament.
Dennis Koch
Dennis Koch8mo ago
Sounds like a different issue then. What does your form look like?
Finn
FinnOP8mo ago
Ah.... 😅 typing this I realized the issue...
protected $hidden = [
'password',
];
protected $hidden = [
'password',
];
Sometimes typing it out already tells you the answer 🙃 So i need to remove the hidden. Can't i unhide something for Filament? Because I would still like to use the hidden on my model as it will also be used in other places rather than Filament 🤔
Dennis Koch
Dennis Koch8mo ago
As you are using a form, you could just add it manually instead of $model->toArray()
Finn
FinnOP8mo ago
I am using it in Filament admin panel's form builder
Dennis Koch
Dennis Koch8mo ago
I assumed you use the standalone one as you didn't select the "Panel" tag. I guess you could use some hydration hook and set ->setHidden([]) on the model
Finn
FinnOP8mo ago
Added the Panel tag so other people who might face the same "issue" will stumble accross this. And thanks for the hydration tip, I will try something out with that ☑️ Thanks!
Finn
FinnOP8mo ago
That would work perfectly, although I am using a relationship field
Grid::make()
->relationship('websiteMailSetting')
->schema([
TextInput::make('password')
->label('Wachtwoord')
->required(),
]),
Grid::make()
->relationship('websiteMailSetting')
->schema([
TextInput::make('password')
->label('Wachtwoord')
->required(),
]),
Which does not resolve through that method
Dennis Koch
Dennis Koch8mo ago
Yeah, those are kinda special, but should have similar methods
Finn
FinnOP8mo ago
Adding this works:
->mutateRelationshipDataBeforeFillUsing(function (array $data) use ($form) {
$data['password'] = $form->getRecord()->websiteMailSetting->password;

return $data;
})
->mutateRelationshipDataBeforeFillUsing(function (array $data) use ($form) {
$data['password'] = $form->getRecord()->websiteMailSetting->password;

return $data;
})
This seems to be the solution, thanks for helping!
Dennis Koch
Dennis Koch8mo ago
Nice.You're welcome
Want results from more Discord servers?
Add your server