diff --git a/README.md b/README.md index 2a89680..9d390a8 100644 --- a/README.md +++ b/README.md @@ -64,3 +64,11 @@ Test method `test_validation_specific_error_shown_in_blade()`. --- +## Task 5. Old Values Staying After Validation Error. + +In `resources/views/teams/create.blade.php` file, the value of "name" field should remain in the form, after failed validation. Change the Blade file so that it would show old value. + +Test method `test_old_value_stays_in_form_after_validation_error()`. + +--- + diff --git a/app/Http/Controllers/TeamController.php b/app/Http/Controllers/TeamController.php new file mode 100644 index 0000000..914c4b2 --- /dev/null +++ b/app/Http/Controllers/TeamController.php @@ -0,0 +1,32 @@ +all(), [ + 'name' => 'required|min:5', + ]); + + if ($validator->fails()) { + return redirect('teams/create') + ->withInput() + ->withErrors($validator); + } + + Team::create($validator->validated()); + + return 'Success'; + } +} diff --git a/app/Models/Team.php b/app/Models/Team.php new file mode 100644 index 0000000..99ab1d1 --- /dev/null +++ b/app/Models/Team.php @@ -0,0 +1,13 @@ +id(); + $table->string('name'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('teams'); + } +} diff --git a/resources/views/teams/create.blade.php b/resources/views/teams/create.blade.php new file mode 100644 index 0000000..2d003c0 --- /dev/null +++ b/resources/views/teams/create.blade.php @@ -0,0 +1,9 @@ +
+ @csrf + Name: +
+ {{-- TASK: change this field so it would contain old value after validation error --}} + +

+ +
diff --git a/routes/web.php b/routes/web.php index b9e690a..9bb7af2 100644 --- a/routes/web.php +++ b/routes/web.php @@ -17,6 +17,7 @@ Route::post('posts', [\App\Http\Controllers\PostController::class, 'store']); Route::post('profile', [\App\Http\Controllers\ProfileController::class, 'update'])->middleware('auth'); Route::resource('projects', \App\Http\Controllers\ProjectController::class); Route::resource('products', \App\Http\Controllers\ProductController::class); +Route::resource('teams', \App\Http\Controllers\TeamController::class); Route::get('/', function () { return view('welcome'); diff --git a/tests/Feature/ValidationTest.php b/tests/Feature/ValidationTest.php index f3e6eb5..450f064 100644 --- a/tests/Feature/ValidationTest.php +++ b/tests/Feature/ValidationTest.php @@ -59,4 +59,12 @@ class ValidationTest extends TestCase $response->assertStatus(200); $response->assertSee('The name field is required.'); } + + public function test_old_value_stays_in_form_after_validation_error() + { + // Post without name should fail + $response = $this->followingRedirects()->post('teams', ['name' => 'Abc']); + $response->assertStatus(200); + $response->assertSee('Abc'); + } }