From 28c2dd8d606ae439c216c69a755dd514b576072e Mon Sep 17 00:00:00 2001 From: PovilasKorop Date: Mon, 29 Nov 2021 16:06:54 +0200 Subject: [PATCH] Task 9 - your own validation rule --- README.md | 8 +++++ app/Http/Controllers/ArticleController.php | 21 ++++++++++++ app/Models/Article.php | 13 ++++++++ ...021_11_29_135338_create_articles_table.php | 32 +++++++++++++++++++ routes/web.php | 1 + tests/Feature/ValidationTest.php | 5 +++ 6 files changed, 80 insertions(+) create mode 100644 app/Http/Controllers/ArticleController.php create mode 100644 app/Models/Article.php create mode 100644 database/migrations/2021_11_29_135338_create_articles_table.php diff --git a/README.md b/README.md index fd34915..aaa3d5d 100644 --- a/README.md +++ b/README.md @@ -96,3 +96,11 @@ Test method `test_custom_error_message()`. --- +## Task 9. Your Own Validation Rule. + +In `app/Http/Controllers/ArticleController.php` file, in `store` method, the code uses `Uppercase` validation rule that you need to create with Artisan command, and fill in with the rule of "title" having first letter as uppercase. + +Test method `test_custom_validation_rule()`. + +--- + diff --git a/app/Http/Controllers/ArticleController.php b/app/Http/Controllers/ArticleController.php new file mode 100644 index 0000000..a195539 --- /dev/null +++ b/app/Http/Controllers/ArticleController.php @@ -0,0 +1,21 @@ +validate([ + // TASK: create your own validation rule called Uppercase + // It should check whether title's first letter is uppercase + 'title' => ['required', new Uppercase()] + ]); + + Article::create(['title' => $request->title]); + } +} diff --git a/app/Models/Article.php b/app/Models/Article.php new file mode 100644 index 0000000..fc94184 --- /dev/null +++ b/app/Models/Article.php @@ -0,0 +1,13 @@ +id(); + $table->string('title'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('articles'); + } +} diff --git a/routes/web.php b/routes/web.php index 53e67ec..9c474eb 100644 --- a/routes/web.php +++ b/routes/web.php @@ -21,6 +21,7 @@ Route::resource('teams', \App\Http\Controllers\TeamController::class); Route::resource('items', \App\Http\Controllers\ItemController::class); Route::put('users/{user}', [\App\Http\Controllers\UserController::class, 'update']); Route::resource('buildings', \App\Http\Controllers\BuildingController::class); +Route::resource('articles', \App\Http\Controllers\ArticleController::class); Route::get('/', function () { return view('welcome'); diff --git a/tests/Feature/ValidationTest.php b/tests/Feature/ValidationTest.php index 09670d0..afc187e 100644 --- a/tests/Feature/ValidationTest.php +++ b/tests/Feature/ValidationTest.php @@ -104,4 +104,9 @@ class ValidationTest extends TestCase $response->assertSee('Please enter the name'); } + public function test_custom_validation_rule() + { + $response = $this->post('articles', ['title' => 'lowercase']); + $response->assertSessionHasErrors('title')->assertStatus(302); + } }