From 2a0d47ee349d7b09130deaecaad2da8397c11915 Mon Sep 17 00:00:00 2001 From: Alex Popa Date: Wed, 11 Dec 2024 13:32:16 +0200 Subject: [PATCH 1/2] Children fields --- .../Pages/EditChildrenIdentity.php | 24 +++++++++++---- app/Models/Children.php | 9 +++++- ...1_drop_age_column_from_childrens_table.php | 30 +++++++++++++++++++ 3 files changed, 57 insertions(+), 6 deletions(-) create mode 100644 database/migrations/2024_12_11_113011_drop_age_column_from_childrens_table.php diff --git a/app/Filament/Organizations/Resources/BeneficiaryResource/Pages/EditChildrenIdentity.php b/app/Filament/Organizations/Resources/BeneficiaryResource/Pages/EditChildrenIdentity.php index f9ab5d79..783e577e 100644 --- a/app/Filament/Organizations/Resources/BeneficiaryResource/Pages/EditChildrenIdentity.php +++ b/app/Filament/Organizations/Resources/BeneficiaryResource/Pages/EditChildrenIdentity.php @@ -11,6 +11,7 @@ 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; @@ -132,18 +133,31 @@ public static function getChildrenIdentityFormSchema(): array ->label(__('field.child_name')) ->maxLength(70), + DatePicker::make('birthdate') + ->label(__('field.birthdate')) + ->maxDate(now()) + ->afterStateUpdated(function (Set $set, $state) { + if (! $state) { + return; + } + + $age = Carbon::parse($state)->diffInYears(now()); + + 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), diff --git a/app/Models/Children.php b/app/Models/Children.php index 48dade29..90dd74a7 100644 --- a/app/Models/Children.php +++ b/app/Models/Children.php @@ -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; @@ -16,7 +17,6 @@ class Children extends Model protected $fillable = [ 'name', - 'age', 'birthdate', 'current_address', 'status', @@ -27,4 +27,11 @@ 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; + } } diff --git a/database/migrations/2024_12_11_113011_drop_age_column_from_childrens_table.php b/database/migrations/2024_12_11_113011_drop_age_column_from_childrens_table.php new file mode 100644 index 00000000..81f4e750 --- /dev/null +++ b/database/migrations/2024_12_11_113011_drop_age_column_from_childrens_table.php @@ -0,0 +1,30 @@ +dropColumn('age'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('childrens', function (Blueprint $table) { + // + }); + } +}; From 6aca53a4c9ae7b53e4e41c23a5573ea367cb595d Mon Sep 17 00:00:00 2001 From: Alex Popa Date: Wed, 11 Dec 2024 23:17:41 +0200 Subject: [PATCH 2/2] Date input for birthdate --- .../Pages/EditChildrenIdentity.php | 15 ++++++--- app/Forms/Components/DateInput.php | 31 +++++++++++++++++++ app/Models/Children.php | 6 ++++ 3 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 app/Forms/Components/DateInput.php diff --git a/app/Filament/Organizations/Resources/BeneficiaryResource/Pages/EditChildrenIdentity.php b/app/Filament/Organizations/Resources/BeneficiaryResource/Pages/EditChildrenIdentity.php index 783e577e..e7c1436f 100644 --- a/app/Filament/Organizations/Resources/BeneficiaryResource/Pages/EditChildrenIdentity.php +++ b/app/Filament/Organizations/Resources/BeneficiaryResource/Pages/EditChildrenIdentity.php @@ -7,7 +7,7 @@ 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; @@ -133,15 +133,22 @@ public static function getChildrenIdentityFormSchema(): array ->label(__('field.child_name')) ->maxLength(70), - DatePicker::make('birthdate') + DateInput::make('birthdate') ->label(__('field.birthdate')) - ->maxDate(now()) ->afterStateUpdated(function (Set $set, $state) { if (! $state) { return; } - $age = Carbon::parse($state)->diffInYears(now()); + try { + $age = Carbon::createFromFormat('d-m-Y', $state)->diffInYears(now()); + } catch (\Exception $e) { + return; + } + + if ($age > 1000) { + return; + } if ($age === 0) { $age = '<1'; diff --git a/app/Forms/Components/DateInput.php b/app/Forms/Components/DateInput.php new file mode 100644 index 00000000..f0366090 --- /dev/null +++ b/app/Forms/Components/DateInput.php @@ -0,0 +1,31 @@ +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'), + ])); + } + }, + ]); + } +} diff --git a/app/Models/Children.php b/app/Models/Children.php index 90dd74a7..58b93b3a 100644 --- a/app/Models/Children.php +++ b/app/Models/Children.php @@ -34,4 +34,10 @@ public function getAgeAttribute(): int | string | 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'); + } }