From acb9bc9bbe33187997beebbbbb7073059a4d467b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Fri, 22 Oct 2021 12:27:48 +0200 Subject: [PATCH] Fix project edit and Site edit --- app/Models/Project.php | 13 ++++++++++++- app/Models/Site.php | 21 ++++++++++++++++++--- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/app/Models/Project.php b/app/Models/Project.php index bd762a1..72cfd06 100644 --- a/app/Models/Project.php +++ b/app/Models/Project.php @@ -32,7 +32,18 @@ class Project extends Model 'validate' => [ 'string', 'max:255', - 'unique:sites,name' + // @phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed + function ($attribute, $value, $fail) { + $q = static::query(); + if (request()->project) { + $q = $q->where('id', '!=', $this->id); + } + if ($q->where('name', $value)->exists()) { + $fail(__(':attribute est déjà utilisé', [ + 'attribute' => $attribute + ])); + } + } ], 'required' => true, 'nullable' => false, diff --git a/app/Models/Site.php b/app/Models/Site.php index 97611bc..5b5820d 100644 --- a/app/Models/Site.php +++ b/app/Models/Site.php @@ -27,6 +27,10 @@ class Site extends Model 'description', 'todo' ]; + protected $attributes = [ + 'status' => '', + ]; + public function __construct($attributes = []) { self::$editableProperties = [ @@ -36,7 +40,18 @@ class Site extends Model 'validate' => [ 'string', 'max:255', - 'unique:sites,name' + // @phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed + function ($attribute, $value, $fail) { + $q = static::query(); + if (request()->site) { + $q = $q->where('id', '!=', $this->id); + } + if ($q->where('name', $value)->exists()) { + $fail(__(':attribute est déjà utilisé', [ + 'attribute' => $attribute + ])); + } + } ], 'required' => true, 'nullable' => false, @@ -54,8 +69,8 @@ class Site extends Model 'string', 'max:255' ], - 'required' => true, - 'nullable' => false, + 'required' => false, + 'nullable' => true, // n'est pas vraiment nullable en base 'actions' => [ CrudAction::view(), CrudAction::create(),