Using protected properties on form actions

Is it safe to store a protected property on a form action?
class MyFormAction extends Action
{
protected bool $myOption = false;

protected function setUp(): void
{
parent::setUp();

$this->label('My Action')
->action(function () {
if ($this->myOption) {
// ...
} else {
// ...
}
});
}

public function myOption(bool $myOption = true): static
{
$this->myOption = $myOption;

return $this;
}
}
class MyFormAction extends Action
{
protected bool $myOption = false;

protected function setUp(): void
{
parent::setUp();

$this->label('My Action')
->action(function () {
if ($this->myOption) {
// ...
} else {
// ...
}
});
}

public function myOption(bool $myOption = true): static
{
$this->myOption = $myOption;

return $this;
}
}
5 Replies
Patrick Boivin
Patrick BoivinOP15mo ago
I was trying to do the same with action arguments but I'm running into an issue where the arguments are not always preserved across Livewire requests:
protected function setUp(): void
{
parent::setUp();

$this->label('My Action')
->action(function () {
$arguments = $this->getArguments();

if ($arguments['myOption'] ?? false) {
// ...
} else {
// ...
}
});
}

public function myOption(bool $myOption = true): static
{
$this->arguments(['myOption' => $myOption]);

return $this;
}
protected function setUp(): void
{
parent::setUp();

$this->label('My Action')
->action(function () {
$arguments = $this->getArguments();

if ($arguments['myOption'] ?? false) {
// ...
} else {
// ...
}
});
}

public function myOption(bool $myOption = true): static
{
$this->arguments(['myOption' => $myOption]);

return $this;
}
Maybe I misunderstood the purpose of action arguments 😅
cheesegrits
cheesegrits15mo ago
I think @Mark Chaney may have some insight on this. I remember him battling action arguments, but I never really understood what was going on.
awcodes
awcodes15mo ago
Arguments aren’t preserved. They have to be passed each time the action is called because they are part of the livewire callback method. If that makes sense. And you can use protected properties. They just won’t be exposed to the front end. So you can’t use them in your views. If you need default arguments though you can define them in the set up method with ->arguments()
cheesegrits
cheesegrits15mo ago
Ah, that makes sense. I hit this in my Maps plugin, with an action I call from JS with: $wire.mountAction('markerAction', {model_id: marker.model_id}) ... which opens an Infolist modal, getting the record with record(function ($argument) { ... }). Worked fine opening the modal, but when the modal is closed, Filament calls record() again with no $arguments. Easy fix, I just nullsafe my code in record(), but I was kinda confused as to why it got called on close with no $arguments.
Patrick Boivin
Patrick BoivinOP15mo ago
Great, that fits my use-case quite well. I'm just trying to provide an "API" to the user of the Action to preconfigure it in the Form. I'm accessing the values only within ->action(). Thank you both 🙂
Want results from more Discord servers?
Add your server