Skip to content

Commit

Permalink
Merge pull request #404 from code4romania/392-update-dob-and-age-fiel…
Browse files Browse the repository at this point in the history
…ds-for-children-table

Children fields
  • Loading branch information
gheorghelupu17 authored Dec 12, 2024
2 parents 2589466 + 6aca53a commit c393274
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
use App\Concerns\RedirectToIdentity;
use App\Enums\GenderShortValues;
use App\Filament\Organizations\Resources\BeneficiaryResource;
use App\Forms\Components\DatePicker;
use App\Forms\Components\DateInput;
use App\Forms\Components\Select;
use App\Forms\Components\TableRepeater;
use App\Services\Breadcrumb\BeneficiaryBreadcrumb;
use Carbon\Carbon;
use Filament\Forms\Components\Checkbox;
use Filament\Forms\Components\Grid;
use Filament\Forms\Components\Section;
Expand Down Expand Up @@ -132,18 +133,38 @@ public static function getChildrenIdentityFormSchema(): array
->label(__('field.child_name'))
->maxLength(70),

DateInput::make('birthdate')
->label(__('field.birthdate'))
->afterStateUpdated(function (Set $set, $state) {
if (! $state) {
return;
}

try {
$age = Carbon::createFromFormat('d-m-Y', $state)->diffInYears(now());
} catch (\Exception $e) {
return;
}

if ($age > 1000) {
return;
}

if ($age === 0) {
$age = '<1';
}
$set('age', $age);
})
->live(),

TextInput::make('age')
->label(__('field.age'))
->mask('99')
->maxLength(2),
->disabled(),

Select::make('gender')
->label(__('field.gender'))
->options(GenderShortValues::options()),

DatePicker::make('birthdate')
->label(__('field.birthdate')),

TextInput::make('current_address')
->label(__('field.current_address'))
->maxLength(70),
Expand Down
31 changes: 31 additions & 0 deletions app/Forms/Components/DateInput.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace App\Forms\Components;

use Carbon\Carbon;
use Closure;
use Filament\Forms\Components\TextInput;

class DateInput extends TextInput
{
protected function setUp(): void
{
parent::setUp();

$this->placeholder('dd-mm-yyyy');
$this->mask('99-99-9999');
$this->formatStateUsing(fn (string $state) => $state ? Carbon::parse($state)->format('d-m-Y') : null);
$this->rules([
'date_format:d-m-Y',
fn (): Closure => function (string $attribute, $value, Closure $fail) {
if (Carbon::createFromFormat('d-m-Y', $value)->greaterThan(now())) {
$fail(__('validation.before_or_equal', [
'date' => now()->format('d-m-Y'),
]));
}
},
]);
}
}
15 changes: 14 additions & 1 deletion app/Models/Children.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use App\Concerns\BelongsToBeneficiary;
use App\Enums\GenderShortValues;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

Expand All @@ -16,7 +17,6 @@ class Children extends Model

protected $fillable = [
'name',
'age',
'birthdate',
'current_address',
'status',
Expand All @@ -27,4 +27,17 @@ class Children extends Model
protected $casts = [
'gender' => GenderShortValues::class,
];

public function getAgeAttribute(): int | string | null
{
$age = $this->birthdate ? Carbon::parse($this->birthdate)->diffInYears(now()) : null;

return $age === 0 ? '<1' : $age;
}

public function setBirthdateAttribute(?string $value = null): void
{
$date = Carbon::createFromFormat('d-m-Y', $value);
$this->attributes['birthdate'] = $date->format('Y-m-d');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('childrens', function (Blueprint $table) {
$table->dropColumn('age');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('childrens', function (Blueprint $table) {
//
});
}
};

0 comments on commit c393274

Please sign in to comment.