From 299817069edab5357c065630ccbc606cbef141b5 Mon Sep 17 00:00:00 2001 From: aslan Date: Tue, 12 Mar 2024 15:32:18 +0300 Subject: [PATCH] add bootstrap validation and Logging for Education Level --- .../Direction/EducationLevelController.php | 48 +++++++++++++++++-- .../Direction/StoreEducationLevelRequest.php | 17 ++++++- .../Direction/UpdateEducationLevelRequest.php | 15 +++++- lang/ru/tooltips.php | 5 ++ .../education_level/create.blade.php | 24 ++++++---- .../direction/education_level/edit.blade.php | 22 +++++---- 6 files changed, 105 insertions(+), 26 deletions(-) diff --git a/app/Http/Controllers/admin/Catalog/Direction/EducationLevelController.php b/app/Http/Controllers/admin/Catalog/Direction/EducationLevelController.php index 089d825..ccfabee 100644 --- a/app/Http/Controllers/admin/Catalog/Direction/EducationLevelController.php +++ b/app/Http/Controllers/admin/Catalog/Direction/EducationLevelController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers\admin\Catalog\Direction; +use App\Helpers\SlugHelper; use App\Http\Controllers\Controller; use App\Http\Requests\admin\Catalog\Direction\StoreEducationLevelRequest; use App\Http\Requests\admin\Catalog\Direction\UpdateEducationLevelRequest; @@ -10,6 +11,8 @@ use Illuminate\Contracts\View\Factory; use Illuminate\Contracts\View\View; use Illuminate\Foundation\Application; use Illuminate\Http\RedirectResponse; +use Illuminate\Support\Facades\Auth; +use Illuminate\Support\Facades\Log; class EducationLevelController extends Controller { @@ -27,18 +30,25 @@ class EducationLevelController extends Controller public function store(StoreEducationLevelRequest $request): RedirectResponse { $validated = $request->validated(); + $slug = SlugHelper::get($validated); + $level = new EducationLevel(); $level->name = $validated['name']; $level->description = $validated['description']; - $level->slug = $validated['slug']; + $level->slug = $slug; $level->save(); + Log::channel('app') + ->info( + 'CREATE уровень образования {educationLevel} - user {user}', + ['user' => Auth::user()->name, 'educationLevel' => $level->name, 'data' => $validated] + ); + return redirect()->route('education_levels.index'); } - public function show( - EducationLevel $educationLevel - ): View|Application|Factory|\Illuminate\Contracts\Foundation\Application { + public function show(EducationLevel $educationLevel): View + { $directions = $educationLevel->directions(); return view( 'admin.catalog.direction.education_level.show', @@ -54,10 +64,22 @@ class EducationLevelController extends Controller public function update(UpdateEducationLevelRequest $request, EducationLevel $educationLevel): RedirectResponse { $validated = $request->validated(); + $oldData = $educationLevel->toArray(); $educationLevel->name = $validated['name']; $educationLevel->description = $validated['description']; $educationLevel->slug = $validated['slug']; + + Log::channel('app') + ->warning( + 'UPDATE уровень образования {educationLevel} - user {user}', + [ + 'user' => Auth::user()->name, + 'educationLevel' => $educationLevel->name, + 'oldData' => $oldData, + 'newData' => $validated + ] + ); $educationLevel->save(); return redirect()->route('education_levels.index'); @@ -66,8 +88,26 @@ class EducationLevelController extends Controller public function destroy(EducationLevel $educationLevel) { if ($educationLevel->directions()->exists()) { + Log::channel('app') + ->error( + 'NOT DELETE уровень образования {educationLevel} - user {user}', + [ + 'user' => Auth::user()->name, + 'educationLevel' => $educationLevel->name, + 'data' => $educationLevel->toArray(), + ] + ); return back(); } + Log::channel('app') + ->critical( + 'DELETE уровень образования {educationLevel} - user {user}', + [ + 'user' => Auth::user()->name, + 'educationLevel' => $educationLevel->name, + 'data' => $educationLevel->toArray(), + ] + ); $educationLevel->delete(); return redirect()->route('education_levels.index'); } diff --git a/app/Http/Requests/admin/Catalog/Direction/StoreEducationLevelRequest.php b/app/Http/Requests/admin/Catalog/Direction/StoreEducationLevelRequest.php index 0e28fb6..5a21e49 100644 --- a/app/Http/Requests/admin/Catalog/Direction/StoreEducationLevelRequest.php +++ b/app/Http/Requests/admin/Catalog/Direction/StoreEducationLevelRequest.php @@ -15,8 +15,21 @@ class StoreEducationLevelRequest extends FormRequest { return [ 'name' => 'required|string|max:255|unique:education_levels,name', - 'description' => 'string', - 'slug' => 'required|string|max:255|unique:education_levels,slug', + 'description' => 'nullable|string', + 'slug' => 'nullable|string|max:255|unique:education_levels,slug', + ]; + } + + public function messages(): array + { + return [ + 'name.required' => 'Поле название обязательно.', + 'name.string' => 'Поле название должен быть строкой.', + 'name.max' => 'Поле название не должен превышать :max символов.', + 'name.unique' => 'Название уже занят.', + 'description.string' => 'Поле описание должен быть строкой.', + 'slug.string' => 'Поле URL должен быть строкой.', + 'slug.max' => 'Поле URL не должен превышать :max символов.', ]; } } diff --git a/app/Http/Requests/admin/Catalog/Direction/UpdateEducationLevelRequest.php b/app/Http/Requests/admin/Catalog/Direction/UpdateEducationLevelRequest.php index a11023c..9948307 100644 --- a/app/Http/Requests/admin/Catalog/Direction/UpdateEducationLevelRequest.php +++ b/app/Http/Requests/admin/Catalog/Direction/UpdateEducationLevelRequest.php @@ -15,8 +15,21 @@ class UpdateEducationLevelRequest extends FormRequest { return [ 'name' => "required|string|max:255|unique:education_levels,name,{$this->education_level->id}", - 'description' => 'string', + 'description' => 'nullable|string', 'slug' => "required|string|max:255|unique:education_levels,slug,{$this->education_level->id}", ]; } + + public function messages(): array + { + return [ + 'name.required' => 'Поле название обязательно.', + 'name.string' => 'Поле название должен быть строкой.', + 'name.max' => 'Поле название не должен превышать :max символов.', + 'name.unique' => 'Название уже занят.', + 'description.string' => 'Поле описание должен быть строкой.', + 'slug.string' => 'Поле URL должен быть строкой.', + 'slug.max' => 'Поле URL не должен превышать :max символов.', + ]; + } } diff --git a/lang/ru/tooltips.php b/lang/ru/tooltips.php index e66ef29..14782d5 100644 --- a/lang/ru/tooltips.php +++ b/lang/ru/tooltips.php @@ -64,4 +64,9 @@ return [ 'description' => 'Поле "Описание" может быть пустым для отображения на сайте', 'slug' => 'Поле "URL" нужно для отображения в браузере' ], + 'education_levels' => [ + 'name' => 'Поле "Название" должно быть уникальным для отображения на сайте', + 'description' => 'Поле "Описание" может быть пустым для отображения на сайте', + 'slug' => 'Поле "URL" нужно для отображения в браузере' + ], ]; diff --git a/resources/views/admin/catalog/direction/education_level/create.blade.php b/resources/views/admin/catalog/direction/education_level/create.blade.php index 59a646e..611f0b0 100644 --- a/resources/views/admin/catalog/direction/education_level/create.blade.php +++ b/resources/views/admin/catalog/direction/education_level/create.blade.php @@ -4,39 +4,43 @@

Создать Уровень Образования

- {{ Form::open(['url' => route('education_levels.store'), 'method' => 'POST', 'class' => '']) }} + {{ Form::open(['url' => route('education_levels.store'), 'method' => 'POST', 'class' => 'needs-validation', 'novalidate']) }}
- {{ Form::label('name', 'Название') }} + {{ Form::label('name', 'Название', ['data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.education_levels.name')]) }} + *
- {{ Form::text('name', '', ['class' => 'form-control']) }} + {{ Form::text('name', '', ['class' => 'form-control', 'data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.education_levels.name'), 'required']) }} +
+ Поле "Название" обязательно! +
-
+
@if ($errors->any()) {{ $errors->first('name') }} @endif
- {{ Form::label('description', 'Описание') }} + {{ Form::label('description', 'Описание', ['data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.education_levels.description')]) }}
- {{ Form::text('description', '', ['class' => 'form-control']) }} + {{ Form::text('description', '', ['class' => 'form-control', 'data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.education_levels.description')]) }}
-
+
@if ($errors->any()) {{ $errors->first('description') }} @endif
- {{ Form::label('slug', 'URL') }} + {{ Form::label('slug', 'URL', ['data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.education_levels.slug')]) }}
- {{ Form::text('slug', '', ['class' => 'form-control']) }} + {{ Form::text('slug', '', ['class' => 'form-control', 'data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.education_levels.slug')]) }}
-
+
@if ($errors->any()) {{ $errors->first('slug') }} @endif diff --git a/resources/views/admin/catalog/direction/education_level/edit.blade.php b/resources/views/admin/catalog/direction/education_level/edit.blade.php index 868fcb5..73a620c 100644 --- a/resources/views/admin/catalog/direction/education_level/edit.blade.php +++ b/resources/views/admin/catalog/direction/education_level/edit.blade.php @@ -5,39 +5,43 @@

Езменить уровень образования

- {{ Form::open(['url' => route('education_levels.update', $educationLevel), 'method' => 'PATCH', 'class' => '']) }} + {{ Form::open(['url' => route('education_levels.update', $educationLevel), 'method' => 'PATCH', 'class' => 'needs-validation', 'novalidate']) }}
- {{ Form::label('name', 'Название') }} + {{ Form::label('name', 'Название', ['data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.faculty.name')]) }} + *
{{ Form::text('name', $educationLevel->name, ['class' => 'form-control']) }} +
+ Поле "Название" обязательно! +
-
+
@if ($errors->any()) {{ $errors->first('name') }} @endif
- {{ Form::label('description', 'Описание') }} + {{ Form::label('description', 'Описание', ['data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.faculty.description')]) }}
- {{ Form::text('description', $educationLevel->description, ['class' => 'form-control']) }} + {{ Form::text('description', $educationLevel->description, ['class' => 'form-control', 'data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.faculty.description')]) }}
-
+
@if ($errors->any()) {{ $errors->first('description') }} @endif
- {{ Form::label('slug', 'URL') }} + {{ Form::label('slug', 'URL', ['data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.faculty.slug')]) }}
- {{ Form::text('slug', $educationLevel->slug, ['class' => 'form-control']) }} + {{ Form::text('slug', $educationLevel->slug, ['class' => 'form-control','data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.faculty.slug')]) }}
-
+
@if ($errors->any()) {{ $errors->first('slug') }} @endif