From a13fd5a567868fe7339f8b04e609a5db6abda156 Mon Sep 17 00:00:00 2001 From: Abdalrhman-Jabri Date: Thu, 26 Sep 2024 11:00:30 +0300 Subject: [PATCH] My solution --- app/Http/Controllers/BuildingController.php | 2 +- app/Http/Controllers/ItemController.php | 3 --- app/Http/Controllers/PostController.php | 7 +++-- app/Http/Controllers/ProfileController.php | 2 ++ app/Http/Controllers/UserController.php | 3 ++- app/Http/Requests/StoreBuildingRequest.php | 6 +++++ app/Http/Requests/StoreItemRequest.php | 29 +++++++++++++++++++++ app/Rules/Uppercase.php | 21 +++++++++++++++ resources/views/products/create.blade.php | 6 +++++ resources/views/projects/create.blade.php | 11 +++++++- resources/views/teams/create.blade.php | 2 +- 11 files changed, 81 insertions(+), 11 deletions(-) create mode 100644 app/Http/Requests/StoreItemRequest.php create mode 100644 app/Rules/Uppercase.php diff --git a/app/Http/Controllers/BuildingController.php b/app/Http/Controllers/BuildingController.php index 9c752ab8..875b4cbb 100644 --- a/app/Http/Controllers/BuildingController.php +++ b/app/Http/Controllers/BuildingController.php @@ -17,7 +17,7 @@ public function create() // TASK: Customize the validation error message to say "Please enter the name" public function store(StoreBuildingRequest $request) { - Building::create($validator->validated()); + Building::create($request->validated()); return 'Success'; } diff --git a/app/Http/Controllers/ItemController.php b/app/Http/Controllers/ItemController.php index 7c8082ae..2252e294 100644 --- a/app/Http/Controllers/ItemController.php +++ b/app/Http/Controllers/ItemController.php @@ -7,9 +7,6 @@ class ItemController extends Controller { - // TASK: generate that StoreItemRequest class - // with authorization returning TRUE - // and validation rules: name is required, and description is required public function store(StoreItemRequest $request) { Item::create($request->validated()); diff --git a/app/Http/Controllers/PostController.php b/app/Http/Controllers/PostController.php index cd0808b7..6548af1c 100644 --- a/app/Http/Controllers/PostController.php +++ b/app/Http/Controllers/PostController.php @@ -9,10 +9,9 @@ class PostController extends Controller { public function store(Request $request) { - $request->validate( - // ... TASK: write validation here so that "title" field - // would be required and unique in the "posts" DB table - ); + $request->validate([ + 'title' => 'required|unique:posts' + ]); // 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..deaad248 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -9,9 +9,10 @@ class UserController extends Controller { public function update(User $user, UpdateUserRequest $request) { + // dd($request->all()); // 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->validated()); return 'Success'; } diff --git a/app/Http/Requests/StoreBuildingRequest.php b/app/Http/Requests/StoreBuildingRequest.php index fbd8c064..0f70c00d 100644 --- a/app/Http/Requests/StoreBuildingRequest.php +++ b/app/Http/Requests/StoreBuildingRequest.php @@ -30,4 +30,10 @@ public function rules() 'name' => 'required' ]; } + public function messages() + { + 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..1f0f9c9c --- /dev/null +++ b/app/Http/Requests/StoreItemRequest.php @@ -0,0 +1,29 @@ +|string> + */ + 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 @@ +
+ + @error('name') +
+

{{ $message }}

+
+ @enderror {{-- TASK: show the validation error for the specific "name" field --}} {{-- using one Blade directive: pseudo-code below --}} {{-- @directive --}} diff --git a/resources/views/projects/create.blade.php b/resources/views/projects/create.blade.php index dc19b63b..4e33bf12 100644 --- a/resources/views/projects/create.blade.php +++ b/resources/views/projects/create.blade.php @@ -3,7 +3,16 @@ {{-- TASK: add the validation errors here - with whatever HTML structure you want --}} {{-- in case of title/description empty, visitor should see --}} {{-- "The name field is required." and "The description field is required." --}} - +@if ($errors->any){ + + } +@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 --}} - +