From 7f2f24ca59fe69f57983bf792457875f7074c165 Mon Sep 17 00:00:00 2001 From: aslan Date: Wed, 13 Mar 2024 11:24:10 +0300 Subject: [PATCH] add bootstrap validation and Logging for Educational Institution --- .../EducationalInstitutionController.php | 43 +++++++++++++++++ .../StoreEducationalInstitutionRequest.php | 20 +++++++- .../UpdateEducationalInstitutionRequest.php | 19 +++++++- .../educational_institution/create.blade.php | 2 +- .../educational_institution/edit.blade.php | 48 +++++++++++-------- 5 files changed, 108 insertions(+), 24 deletions(-) diff --git a/app/Http/Controllers/admin/Catalog/EducationalInstitutionController.php b/app/Http/Controllers/admin/Catalog/EducationalInstitutionController.php index 2a88084..9ef1e5a 100644 --- a/app/Http/Controllers/admin/Catalog/EducationalInstitutionController.php +++ b/app/Http/Controllers/admin/Catalog/EducationalInstitutionController.php @@ -9,6 +9,8 @@ use App\Models\EducationalInstitution; use Illuminate\Contracts\View\Factory; use Illuminate\Contracts\View\View; use Illuminate\Foundation\Application; +use Illuminate\Support\Facades\Auth; +use Illuminate\Support\Facades\Log; class EducationalInstitutionController extends Controller { @@ -34,6 +36,16 @@ class EducationalInstitutionController extends Controller $educationalInstitution->position = $validated['position']; $educationalInstitution->save(); + Log::channel('app') + ->info( + 'CREATE учеб. заведение {educationalInstitution} - user {user}', + [ + 'user' => Auth::user()->name, + 'educationalInstitution' => $educationalInstitution->name, + 'data' => $validated + ] + ); + return redirect()->route('educational_institutions.index'); } @@ -50,11 +62,24 @@ class EducationalInstitutionController extends Controller public function update(UpdateEducationalInstitutionRequest $request, EducationalInstitution $educationalInstitution) { $validated = $request->validated(); + $oldData = $educationalInstitution->toArray(); $educationalInstitution->name = $validated['name']; $educationalInstitution->description = $validated['description']; $educationalInstitution->position = $validated['position']; $educationalInstitution->slug = $validated['slug']; + + Log::channel('app') + ->warning( + 'UPDATE учеб. заведение {educationalInstitution} - user {user}', + [ + 'user' => Auth::user()->name, + 'faculty' => $educationalInstitution->name, + 'oldData' => $oldData, + 'newData' => $validated + ] + ); + $educationalInstitution->save(); return redirect()->route('educational_institutions.index'); @@ -63,8 +88,26 @@ class EducationalInstitutionController extends Controller public function destroy(EducationalInstitution $educationalInstitution) { if ($educationalInstitution->faculties()->exists()) { + Log::channel('app') + ->error( + 'NOT DELETE учеб. заведение {educationalInstitution} - user {user}', + [ + 'user' => Auth::user()->name, + 'educationalInstitution' => $educationalInstitution->name, + 'data' => $educationalInstitution->toArray(), + ] + ); return back(); } + Log::channel('app') + ->critical( + 'DELETE учеб. заведение {educationalInstitution} - user {user}', + [ + 'user' => Auth::user()->name, + 'educationalInstitution' => $educationalInstitution->name, + 'data' => $educationalInstitution->toArray(), + ] + ); $educationalInstitution->delete(); return redirect()->route('educational_institutions.index'); diff --git a/app/Http/Requests/admin/Catalog/StoreEducationalInstitutionRequest.php b/app/Http/Requests/admin/Catalog/StoreEducationalInstitutionRequest.php index af506f6..5932d61 100644 --- a/app/Http/Requests/admin/Catalog/StoreEducationalInstitutionRequest.php +++ b/app/Http/Requests/admin/Catalog/StoreEducationalInstitutionRequest.php @@ -16,8 +16,24 @@ class StoreEducationalInstitutionRequest extends FormRequest return [ 'position' => 'required|int|numeric|max:255', 'name' => 'required|string|max:255|unique:educational_institutions,name', - 'description' => 'string', - 'slug' => 'required|string|max:255|unique:educational_institutions,slug', + 'description' => 'nullable|string', + 'slug' => 'nullable|string|max:255|unique:educational_institutions,slug', + ]; + } + public function messages(): array + { + return [ + 'position.required' => 'Поле позиция обязательно.', + 'position.int' => 'Позиция должно быть целым числом.', + 'position.numeric' => 'Позиция должно быть числом.', + 'position.max' => 'Позиция не должен быть больше :max', + '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/UpdateEducationalInstitutionRequest.php b/app/Http/Requests/admin/Catalog/UpdateEducationalInstitutionRequest.php index 0111f53..e64268a 100644 --- a/app/Http/Requests/admin/Catalog/UpdateEducationalInstitutionRequest.php +++ b/app/Http/Requests/admin/Catalog/UpdateEducationalInstitutionRequest.php @@ -16,7 +16,7 @@ class UpdateEducationalInstitutionRequest extends FormRequest { return [ 'position' => 'required|int|numeric|max:255', - 'description' => 'string', + 'description' => 'nullable|string', 'slug' => [ 'required', 'string', @@ -31,4 +31,21 @@ class UpdateEducationalInstitutionRequest extends FormRequest ], ]; } + + public function messages(): array + { + return [ + 'position.required' => 'Поле позиция обязательно.', + 'position.int' => 'Позиция должно быть целым числом.', + 'position.numeric' => 'Позиция должно быть числом.', + 'position.max' => 'Позиция не должен быть больше :max', + 'name.required' => 'Поле название обязательно.', + 'name.string' => 'Поле название должен быть строкой.', + 'name.max' => 'Поле название не должен превышать :max символов.', + 'name.unique' => 'Название уже занят.', + 'description.string' => 'Поле описание должен быть строкой.', + 'slug.string' => 'Поле URL должен быть строкой.', + 'slug.max' => 'Поле URL не должен превышать :max символов.', + ]; + } } diff --git a/resources/views/admin/catalog/educational_institution/create.blade.php b/resources/views/admin/catalog/educational_institution/create.blade.php index 0d6b372..1a46833 100644 --- a/resources/views/admin/catalog/educational_institution/create.blade.php +++ b/resources/views/admin/catalog/educational_institution/create.blade.php @@ -28,7 +28,7 @@ *
- {{ Form::text('name', '', ['class' => 'form-control', 'data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.educational_institutions.name'), 'required']) }} + {{ Form::number('name', '', ['class' => 'form-control', 'data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.educational_institutions.name'), 'required']) }}
Поле "Название" обязательно!
diff --git a/resources/views/admin/catalog/educational_institution/edit.blade.php b/resources/views/admin/catalog/educational_institution/edit.blade.php index 3584bc8..0fcdfd8 100644 --- a/resources/views/admin/catalog/educational_institution/edit.blade.php +++ b/resources/views/admin/catalog/educational_institution/edit.blade.php @@ -4,50 +4,58 @@

Изменить учебное заведение

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