From d95517db7645f5c6294ba0a21187b618aa5667f8 Mon Sep 17 00:00:00 2001 From: SaraRamadan25 Date: Mon, 1 Jan 2024 18:20:53 +0200 Subject: [PATCH] All Tests Passed --- app/Http/Controllers/ArticleController.php | 3 ++- app/Http/Controllers/BuildingController.php | 2 +- app/Http/Controllers/PostController.php | 6 ++--- app/Http/Controllers/ProfileController.php | 6 ++--- app/Http/Controllers/UserController.php | 2 +- app/Http/Requests/StoreBuildingRequest.php | 6 +++++ app/Http/Requests/StoreItemRequest.php | 29 +++++++++++++++++++++ app/Rules/Uppercase.php | 17 ++++++++++++ phpunit.xml | 4 +-- resources/views/products/create.blade.php | 8 +++--- resources/views/projects/create.blade.php | 10 +++++++ resources/views/teams/create.blade.php | 2 +- 12 files changed, 78 insertions(+), 17 deletions(-) create mode 100644 app/Http/Requests/StoreItemRequest.php create mode 100644 app/Rules/Uppercase.php diff --git a/app/Http/Controllers/ArticleController.php b/app/Http/Controllers/ArticleController.php index a1955393..d0032b4c 100644 --- a/app/Http/Controllers/ArticleController.php +++ b/app/Http/Controllers/ArticleController.php @@ -5,6 +5,7 @@ use App\Models\Article; use App\Rules\Uppercase; use Illuminate\Http\Request; +use Illuminate\Validation\Rule; class ArticleController extends Controller { @@ -13,7 +14,7 @@ public function store(Request $request) $request->validate([ // TASK: create your own validation rule called Uppercase // It should check whether title's first letter is uppercase - 'title' => ['required', new Uppercase()] + 'title' => ['required', new Uppercase] ]); Article::create(['title' => $request->title]); 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/PostController.php b/app/Http/Controllers/PostController.php index cd0808b7..4ef22cd9 100644 --- a/app/Http/Controllers/PostController.php +++ b/app/Http/Controllers/PostController.php @@ -9,9 +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 diff --git a/app/Http/Controllers/ProfileController.php b/app/Http/Controllers/ProfileController.php index bb6bab39..422a4b02 100644 --- a/app/Http/Controllers/ProfileController.php +++ b/app/Http/Controllers/ProfileController.php @@ -9,10 +9,8 @@ class ProfileController extends Controller public function update(Request $request) { $request->validate([ - // TASK: imagine that in the Blade the fields are - // - // - // 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..a6d0f989 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->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..bda893b8 --- /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..9e6e70e4 --- /dev/null +++ b/app/Rules/Uppercase.php @@ -0,0 +1,17 @@ + - - + + diff --git a/resources/views/products/create.blade.php b/resources/views/products/create.blade.php index 611b7ef6..c94840f2 100644 --- a/resources/views/products/create.blade.php +++ b/resources/views/products/create.blade.php @@ -5,10 +5,10 @@
{{-- TASK: show the validation error for the specific "name" field --}} - {{-- using one Blade directive: pseudo-code below --}} - {{-- @directive --}} - {{-- {{ $message }} --}} - {{-- @endDirective --}} + {{-- using one Blade directive --}} + @error('name') + {{ $message }}

+ @enderror diff --git a/resources/views/projects/create.blade.php b/resources/views/projects/create.blade.php index dc19b63b..2b3b2498 100644 --- a/resources/views/projects/create.blade.php +++ b/resources/views/projects/create.blade.php @@ -16,3 +16,13 @@

+ +@if ($errors->any()) +
+
    + @foreach ($errors->all() as $error) +
  • {{ $error }}
  • + @endforeach +
+
+@endif 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 --}} - +