F
Filament2mo ago
Tieme

Action - utility injection (Get, Set)

Hi All, The actions have some utility injections (https://filamentphp.com/docs/4.x/actions/overview#action-utility-injection) The one that i'm missing at the moment is Get and Set (i'm missing them because i want to use them). For example i have below action What i want to try is that all validation will be handled by the action. I don't want the data to save something, So in the action i have $action->halt() But i can not Set any value in the action. How do i change the view in the action? I have tried the aftervalidate hook, but Get and Set are also not available. I'm happy to here any other solutions.
Action::make('search_by_code')
->label(__('search_by_code'))
->modalwidth(Width::Medium)
->modalCancelAction(false)
->schema([
TextInput::make('referral_code')
->label('referral_code')
->maxLength(11)
->mask('*****-*****')
->placeholder('*****-*****')
->default('12345-ABCDEF') // Todo : Remove
->columnSpanFull()
->required()
->regex('/^(?:\d{5}-[A-Z]{5}|[A-Z]{5}-\d{5})$/i')
->afterStateUpdated(fn (Set $set) => $set('search_view', null)),
Hidden::make('search_view')->dehydrated(false),
View::make('filament.user.resources.companies.actions.referral_code_responce')
->viewData(fn (Get $get) => [
'result' => $get('search_view'),
])
->hidden(fn (Get $get) => blank($get('search_view'))),
])
->action(function (array $data, Action $action) {
$code = strtoupper($data['referral_code']);
$company = Company::query()->where('referral_code', $code)->first();

// This does not work
// $set('search_view', $company
// ? ['status' => 'found', 'company' => ['id' => $company->id, 'name' => $company->name]]
// : ['status' => 'not_found']
// );

$action->halt();
})
Action::make('search_by_code')
->label(__('search_by_code'))
->modalwidth(Width::Medium)
->modalCancelAction(false)
->schema([
TextInput::make('referral_code')
->label('referral_code')
->maxLength(11)
->mask('*****-*****')
->placeholder('*****-*****')
->default('12345-ABCDEF') // Todo : Remove
->columnSpanFull()
->required()
->regex('/^(?:\d{5}-[A-Z]{5}|[A-Z]{5}-\d{5})$/i')
->afterStateUpdated(fn (Set $set) => $set('search_view', null)),
Hidden::make('search_view')->dehydrated(false),
View::make('filament.user.resources.companies.actions.referral_code_responce')
->viewData(fn (Get $get) => [
'result' => $get('search_view'),
])
->hidden(fn (Get $get) => blank($get('search_view'))),
])
->action(function (array $data, Action $action) {
$code = strtoupper($data['referral_code']);
$company = Company::query()->where('referral_code', $code)->first();

// This does not work
// $set('search_view', $company
// ? ['status' => 'found', 'company' => ['id' => $company->id, 'name' => $company->name]]
// : ['status' => 'not_found']
// );

$action->halt();
})
Solution:
i found a not so nice way to do this. ```php Action::make('search_by_code') ->label(__('resource.companies.actions.search_by_code.label'))...
Jump to solution
1 Reply
Solution
Tieme
Tieme2mo ago
i found a not so nice way to do this.
Action::make('search_by_code')
->label(__('resource.companies.actions.search_by_code.label'))
->schema([
TextInput::make('referral_code')
->maxLength(11)
->mask('*****-*****')
->placeholder('*****-*****')
->columnSpanFull()
->required()
->validationMessages([
'regex' => __('resource.companies.actions.search_by_code.validation_messages.regex'),
])
->regex('/^(?:\d{5}-[A-Z]{5}|[A-Z]{5}-\d{5})$/i')
->live()
->afterStateUpdated(function ($livewire) {
foreach ($livewire->getheaderActions() as $action) {
if ($action->getName() == 'search_by_code') {
$action->modalContentFooter(null);
}

}
}),
])
->action(function (array $data, Action $action) {
$code = strtoupper($data['referral_code']);
$company = Company::query()->where('referral_code', $code)->first();

$viewData = $company ? ['status' => 'found', 'company' => ['company' => $company]] : ['status' => 'not_found'];
$action->modalContentFooter(view('filament.user.resources.companies.actions.search_by_code', ['result' => $viewData]));

$action->halt();
})
Action::make('search_by_code')
->label(__('resource.companies.actions.search_by_code.label'))
->schema([
TextInput::make('referral_code')
->maxLength(11)
->mask('*****-*****')
->placeholder('*****-*****')
->columnSpanFull()
->required()
->validationMessages([
'regex' => __('resource.companies.actions.search_by_code.validation_messages.regex'),
])
->regex('/^(?:\d{5}-[A-Z]{5}|[A-Z]{5}-\d{5})$/i')
->live()
->afterStateUpdated(function ($livewire) {
foreach ($livewire->getheaderActions() as $action) {
if ($action->getName() == 'search_by_code') {
$action->modalContentFooter(null);
}

}
}),
])
->action(function (array $data, Action $action) {
$code = strtoupper($data['referral_code']);
$company = Company::query()->where('referral_code', $code)->first();

$viewData = $company ? ['status' => 'found', 'company' => ['company' => $company]] : ['status' => 'not_found'];
$action->modalContentFooter(view('filament.user.resources.companies.actions.search_by_code', ['result' => $viewData]));

$action->halt();
})

Did you find this page helpful?