From 8678ce520bcc7b4b1d6795ed4b5fe299de8ee5ae Mon Sep 17 00:00:00 2001 From: "noah.lancien" Date: Tue, 18 Apr 2023 12:32:46 +0000 Subject: [PATCH] Added validation rules for duty hours --- app/Models/Dutyhour.php | 24 +++++++++++++++++++ ...23_04_18_123853_create_dutyhours_table.php | 2 +- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/app/Models/Dutyhour.php b/app/Models/Dutyhour.php index 05ba786..66f6864 100644 --- a/app/Models/Dutyhour.php +++ b/app/Models/Dutyhour.php @@ -63,6 +63,22 @@ class Dutyhour extends Model 'date' => [ 'label' => trans('Date'), 'type' => CrudType::date(), + 'validate' => [ + 'date', + 'after_or_equal:today', + // @phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed + function ($attribute, $value, $fail) { + $q = static::query(); + if (request()->dutyhour) { + $q = $q->where('id', '!=', $this->id); + } + if ($q->where('date', $value)->exists()) { + $fail(__('La :attribute est déjà utilisée', [ + 'attribute' => $attribute + ])); + } + } + ], 'nullable' => false, 'actions' => [ CrudAction::viewAny(), @@ -74,6 +90,10 @@ class Dutyhour extends Model 'person' => [ 'label' => trans('Personne présente'), 'type' => CrudType::string(), + 'validate' => [ + 'string', + 'max:255' + ], 'nullable' => false, 'actions' => [ CrudAction::viewAny(), @@ -85,6 +105,10 @@ class Dutyhour extends Model 'place' => [ 'label' => trans('Lieu'), 'type' => CrudType::string(), + 'validate' => [ + 'string', + 'max:255' + ], 'nullable' => false, 'actions' => [ CrudAction::viewAny(), diff --git a/database/migrations/2023_04_18_123853_create_dutyhours_table.php b/database/migrations/2023_04_18_123853_create_dutyhours_table.php index d11d2fc..1d8ccb9 100644 --- a/database/migrations/2023_04_18_123853_create_dutyhours_table.php +++ b/database/migrations/2023_04_18_123853_create_dutyhours_table.php @@ -15,7 +15,7 @@ class CreateDutyhoursTable extends Migration { Schema::create('dutyhours', function (Blueprint $table) { $table->id(); - $table->date('date'); + $table->date('date')->unique(); $table->string('person'); $table->string('place'); $table->timestamps();