F
Filament6mo ago
Aminne

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
Tetracyclic6mo ago
What are you trying to achieve?
Aminne
AminneOP6mo 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
AminneOP6mo ago
No description
Aminne
AminneOP6mo ago
How i can handle these errors.
Tetracyclic
Tetracyclic6mo ago
That error is being produced by the laravel-wallet package you're using, not Filament.
Aminne
AminneOP6mo ago
yes how i can handle it this's the problem
Tetracyclic
Tetracyclic6mo 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
AminneOP6mo 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
Tetracyclic6mo ago
The exception name is shown at the top of the image you shared of the error.
Aminne
AminneOP6mo ago
yes thank you.
Aminne
AminneOP6mo ago
Can i implement all these exceptions ? on the same catch
No description
Tetracyclic
Tetracyclic6mo 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
AminneOP6mo ago
aah ok i understand now Thank you for your help.
Tetracyclic
Tetracyclic6mo ago
If you have multiple in the same catch block, you can use $exception instanceof BalanceIsEmpty to test for each type
Aminne
AminneOP6mo 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.
Want results from more Discord servers?
Add your server