How i can custom failure form in custom action

This's my code
Actions\Action::make('newUser')
->action(function (array $data): void {`})->form(function(array data): void {})
Actions\Action::make('newUser')
->action(function (array $data): void {`})->form(function(array data): void {})
15 Replies
Tetracyclic
Tetracyclic3w ago
What are you trying to achieve?
Aminne
Aminne3w ago
Forms\Components\Select::make('from_id')
->relationship(name: 'from',
modifyQueryUsing: fn (Builder $query, Get $get) => $query->where('id', '!=', $get('to_id')))
->live(onBlur: true)->configureUsing(function(Get $get, $state) {
$wallet = Wallet::find($state);
return $wallet->balance == 0;

})
,Forms\Components\TextInput::make('amount')
->numeric()
->minValue(0)
->required()
Forms\Components\Select::make('from_id')
->relationship(name: 'from',
modifyQueryUsing: fn (Builder $query, Get $get) => $query->where('id', '!=', $get('to_id')))
->live(onBlur: true)->configureUsing(function(Get $get, $state) {
$wallet = Wallet::find($state);
return $wallet->balance == 0;

})
,Forms\Components\TextInput::make('amount')
->numeric()
->minValue(0)
->required()
I want to make two validations: check if the wallet is not empty, and amount of wallet is enough to make a transfer from wallet to another by this amount. When i try to send a form i got this error
Aminne
Aminne3w ago
No description
Aminne
Aminne3w ago
How i can handle these errors.
Tetracyclic
Tetracyclic3w ago
That error is being produced by the laravel-wallet package you're using, not Filament.
Aminne
Aminne3w ago
yes how i can handle it this's the problem
Tetracyclic
Tetracyclic3w ago
You'd use a try ... catch statement
try {
// code that triggers the exception
} catch (BalanceIsEmpty $exception) {
// handle the exception
}
try {
// code that triggers the exception
} catch (BalanceIsEmpty $exception) {
// handle the exception
}
But it depends on what bit of your code is triggering the exception and whether the laravel-wallet package provides any other tools for handling those errors. As an aside, it's almost always a bad idea to use a loose comparison operator == instead of strict comparison ===. Especially when dealing with something like financial information. https://abrictosecurity.com/php-type-juggling/
Aminne
Aminne3w ago
I tried this
try {
$transfer = $sender->transfer(
$receiver, $data['amount'],
new Extra(
deposit: new Option(['status' => $data['status'] === 'confirmed' ? 'confirmed' : 'pending'], $data['status'] === 'confirmed'),
withdraw: new Option(['status' => $data['status'] === 'confirmed' ? 'confirmed' : 'pending'], $data['status'] === 'confirmed'),
extra: [
'created_by' => [ 'id' => $user->id, 'name' => $user->name],
'status' => $data['status'] === 'confirmed' ? 'confirmed' : 'pending',
'remark' => $data['remark']
],
)
);
} catch (BalanceIsEmpty $exception) {
Notification::make()
->danger()
->title('The wallet balance of sender is empty')
->send();
}
try {
$transfer = $sender->transfer(
$receiver, $data['amount'],
new Extra(
deposit: new Option(['status' => $data['status'] === 'confirmed' ? 'confirmed' : 'pending'], $data['status'] === 'confirmed'),
withdraw: new Option(['status' => $data['status'] === 'confirmed' ? 'confirmed' : 'pending'], $data['status'] === 'confirmed'),
extra: [
'created_by' => [ 'id' => $user->id, 'name' => $user->name],
'status' => $data['status'] === 'confirmed' ? 'confirmed' : 'pending',
'remark' => $data['remark']
],
)
);
} catch (BalanceIsEmpty $exception) {
Notification::make()
->danger()
->title('The wallet balance of sender is empty')
->send();
}
aah ok good thank you Where did you find this exception? i can return the error message in text input ? before closing the modal Thank you to share
Tetracyclic
Tetracyclic3w ago
The exception name is shown at the top of the image you shared of the error.
Aminne
Aminne3w ago
yes thank you.
Aminne
Aminne3w ago
Can i implement all these exceptions ? on the same catch
No description
Tetracyclic
Tetracyclic3w ago
Yes, you can either add multiple catch blocks, each catching one or more exceptions, or use the union operator to specify multiple exceptions in a single catch:
catch(AmountInvalid|BalanceIsEmpty $exception) {
}
catch(AmountInvalid|BalanceIsEmpty $exception) {
}
Aminne
Aminne3w ago
aah ok i understand now Thank you for your help.
Tetracyclic
Tetracyclic3w ago
If you have multiple in the same catch block, you can use $exception instanceof BalanceIsEmpty to test for each type
Aminne
Aminne3w ago
aah ok good idea.
catch (AmountInvalid|BalanceIsEmpty $exception) {Notification::make()
->danger()->title($exception->getMessage())->send();
}
catch (AmountInvalid|BalanceIsEmpty $exception) {Notification::make()
->danger()->title($exception->getMessage())->send();
}
How i can pass the exception error message from action to form and show it in the body form without closing the modal, with custom rule of text input.