From ed06db9b581416cda7c9280c64b8be9e74465e80 Mon Sep 17 00:00:00 2001 From: Joe Date: Fri, 22 Sep 2023 11:48:12 +0000 Subject: [PATCH] answers --- .gitignore | 2 ++ app/Http/Controllers/PostController.php | 5 ++-- app/Http/Controllers/ProfileController.php | 2 ++ app/Http/Controllers/UserController.php | 2 +- app/Http/Requests/StoreBuildingRequest.php | 7 ++++++ app/Http/Requests/StoreItemRequest.php | 29 ++++++++++++++++++++++ app/Rules/Uppercase.php | 21 ++++++++++++++++ phpunit.xml | 3 +-- resources/views/products/create.blade.php | 3 +++ resources/views/projects/create.blade.php | 10 ++++++++ resources/views/teams/create.blade.php | 2 +- 11 files changed, 80 insertions(+), 6 deletions(-) create mode 100644 app/Http/Requests/StoreItemRequest.php create mode 100644 app/Rules/Uppercase.php diff --git a/.gitignore b/.gitignore index ad69c6fe..685edb9b 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,5 @@ yarn-error.log /.fleet /.idea /.vscode +docker-compose.yml +.devcontainer/devcontainer.json diff --git a/app/Http/Controllers/PostController.php b/app/Http/Controllers/PostController.php index cd0808b7..4500afff 100644 --- a/app/Http/Controllers/PostController.php +++ b/app/Http/Controllers/PostController.php @@ -9,10 +9,11 @@ class PostController extends Controller { public function store(Request $request) { - $request->validate( + $validated = $request->validate([ + 'title' => 'required|unique:posts' + ]); // ... TASK: write validation here so that "title" field // would be required and unique in the "posts" DB table - ); // Saving the post Post::create(['title' => $request->title]); diff --git a/app/Http/Controllers/ProfileController.php b/app/Http/Controllers/ProfileController.php index bb6bab39..8cd35637 100644 --- a/app/Http/Controllers/ProfileController.php +++ b/app/Http/Controllers/ProfileController.php @@ -13,6 +13,8 @@ public function update(Request $request) // // // Write validation rules, so both name and email are required + 'profile.name' => 'required', + 'profile.email' => 'required' ]); auth()->user()->update($request->profile ?? []); diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 404d7471..4047d361 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -11,7 +11,7 @@ public function update(User $user, UpdateUserRequest $request) { // TASK: change this line to not allow is_admin field to be updated // Update only the fields that are validated in UpdateUserRequest - $user->update($request->all()); + $user->update($request->safe()->except(['is_admin'])); return 'Success'; } diff --git a/app/Http/Requests/StoreBuildingRequest.php b/app/Http/Requests/StoreBuildingRequest.php index fbd8c064..5d28bf3e 100644 --- a/app/Http/Requests/StoreBuildingRequest.php +++ b/app/Http/Requests/StoreBuildingRequest.php @@ -30,4 +30,11 @@ public function rules() 'name' => 'required' ]; } + + public function messages(): array + { + return [ + 'name.required' => 'Please enter the name' + ]; + } } diff --git a/app/Http/Requests/StoreItemRequest.php b/app/Http/Requests/StoreItemRequest.php new file mode 100644 index 00000000..85088e05 --- /dev/null +++ b/app/Http/Requests/StoreItemRequest.php @@ -0,0 +1,29 @@ + + */ + public function rules(): array + { + return [ + 'name' => 'required', + 'description' => 'required' + ]; + } +} diff --git a/app/Rules/Uppercase.php b/app/Rules/Uppercase.php new file mode 100644 index 00000000..ac5a1999 --- /dev/null +++ b/app/Rules/Uppercase.php @@ -0,0 +1,21 @@ + - - + diff --git a/resources/views/products/create.blade.php b/resources/views/products/create.blade.php index 611b7ef6..12e0de9c 100644 --- a/resources/views/products/create.blade.php +++ b/resources/views/products/create.blade.php @@ -9,6 +9,9 @@ {{-- @directive --}} {{-- {{ $message }} --}} {{-- @endDirective --}} + @error('name') + {{ $message }} + @enderror

diff --git a/resources/views/projects/create.blade.php b/resources/views/projects/create.blade.php index dc19b63b..0b4feb77 100644 --- a/resources/views/projects/create.blade.php +++ b/resources/views/projects/create.blade.php @@ -4,6 +4,16 @@ {{-- in case of title/description empty, visitor should see --}} {{-- "The name field is required." and "The description field is required." --}} +@if ($errors->any()) +
+
    + @foreach ($errors->all() as $error) +
  • {{ $error }}
  • + @endforeach +
+
+@endif +
@csrf Title: diff --git a/resources/views/teams/create.blade.php b/resources/views/teams/create.blade.php index 2d003c05..f1afdef2 100644 --- a/resources/views/teams/create.blade.php +++ b/resources/views/teams/create.blade.php @@ -3,7 +3,7 @@ Name:
{{-- TASK: change this field so it would contain old value after validation error --}} - +