diff --git a/app/Filament/Resources/TransferResource.php b/app/Filament/Resources/TransferResource.php index a627feb..0273ce3 100644 --- a/app/Filament/Resources/TransferResource.php +++ b/app/Filament/Resources/TransferResource.php @@ -63,7 +63,9 @@ public static function form(Form $form): Form DateTimePicker::make('transacted_at') ->label('Transacted Date') - ->default(now()), + ->default(now()) + ->beforeOrEqual(now()) + ->required(), TextInput::make('amount') ->required() diff --git a/database/factories/TransferFactory.php b/database/factories/TransferFactory.php index 6838996..02f90c3 100644 --- a/database/factories/TransferFactory.php +++ b/database/factories/TransferFactory.php @@ -33,4 +33,11 @@ public function today(): self 'transacted_at' => Carbon::now(), ]); } + + public function tomorrow(): self + { + return $this->state(fn (array $attributes) => [ + 'transacted_at' => Carbon::now()->addDay(), + ]); + } } diff --git a/tests/Feature/Transfer/TransferValidationTest.php b/tests/Feature/Transfer/TransferValidationTest.php index 5946af0..6220de6 100644 --- a/tests/Feature/Transfer/TransferValidationTest.php +++ b/tests/Feature/Transfer/TransferValidationTest.php @@ -54,3 +54,35 @@ 'debtor_id', ]); }); + +it('cannot add future date as transacted_at', function () { + $newData = Transfer::factory()->for($this->user)->tomorrow()->make(); + + livewire(TransferResource\Pages\CreateTransfer::class) + ->fillForm([ + 'debtor_id' => $newData->debtor->id, + 'creditor_id' => $newData->creditor_id, + 'description' => $newData->description, + 'amount' => $newData->amount, + 'transacted_at' => $newData->transacted_at, + ]) + ->call('create') + ->assertHasFormErrors([ + 'transacted_at', + ]); +}); + +it('cannot update future date as transacted_at', function () { + $transfer = Transfer::factory()->for($this->user)->create(); + + livewire(TransferResource\Pages\EditTransfer::class, [ + 'record' => $transfer->getRouteKey(), + ]) + ->fillForm([ + 'transacted_at' => now()->addDay(), + ]) + ->call('save') + ->assertHasFormErrors([ + 'transacted_at', + ]); +});