mirror of
https://github.com/10h30/Test-Laravel-Validation.git
synced 2026-06-05 15:07:56 +09:00
Task 5 - old values staying in the form
This commit is contained in:
@@ -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()`.
|
||||
|
||||
---
|
||||
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Team;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
|
||||
class TeamController extends Controller
|
||||
{
|
||||
public function create()
|
||||
{
|
||||
return view('teams.create');
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
{
|
||||
$validator = Validator::make($request->all(), [
|
||||
'name' => 'required|min:5',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return redirect('teams/create')
|
||||
->withInput()
|
||||
->withErrors($validator);
|
||||
}
|
||||
|
||||
Team::create($validator->validated());
|
||||
|
||||
return 'Success';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Team extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = ['name'];
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class CreateTeamsTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('teams', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('name');
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('teams');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<form method="POST" action="{{ route('teams.store') }}">
|
||||
@csrf
|
||||
Name:
|
||||
<br />
|
||||
{{-- TASK: change this field so it would contain old value after validation error --}}
|
||||
<input type="text" name="name" />
|
||||
<br /><br />
|
||||
<button type="submit">Save</button>
|
||||
</form>
|
||||
@@ -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');
|
||||
|
||||
@@ -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');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user