mirror of
https://github.com/10h30/Test-Laravel-Validation.git
synced 2026-06-05 15:07:56 +09:00
Task 3 - errors shown in validation Blade
This commit is contained in:
@@ -48,3 +48,11 @@ Test method `test_array_validation()`.
|
||||
|
||||
---
|
||||
|
||||
## Task 3. Showing Validation Errors.
|
||||
|
||||
In `resources/views/projects/create.blade.php` file, show the validation errors, for `"name" => "required", "description" => "required"` rules. Use whatever HTML structure you want, like `<ul><li>error</li><li>error 2</li></ul>`. No design needed, the test will just check if the error messages are present.
|
||||
|
||||
Test method `test_validation_errors_shown_in_blade()`.
|
||||
|
||||
---
|
||||
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Project;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
|
||||
class ProjectController extends Controller
|
||||
{
|
||||
public function create()
|
||||
{
|
||||
return view('projects.create');
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
{
|
||||
$validator = Validator::make($request->all(), [
|
||||
'name' => 'required',
|
||||
'description' => 'required'
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return redirect('projects/create')
|
||||
->withErrors($validator);
|
||||
}
|
||||
|
||||
Project::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 Project extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = ['name', 'description'];
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class CreateProjectsTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('projects', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('name');
|
||||
$table->string('description');
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('projects');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
{{-- Form without any design --}}
|
||||
|
||||
{{-- 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." --}}
|
||||
|
||||
<form method="POST" action="{{ route('projects.store') }}">
|
||||
@csrf
|
||||
Title:
|
||||
<br />
|
||||
<input type="text" name="title" />
|
||||
<br /><br />
|
||||
Description:
|
||||
<br />
|
||||
<input type="text" name="description" />
|
||||
<br /><br />
|
||||
<button type="submit">Save</button>
|
||||
</form>
|
||||
@@ -15,6 +15,7 @@ use Illuminate\Support\Facades\Route;
|
||||
|
||||
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::get('/', function () {
|
||||
return view('welcome');
|
||||
|
||||
@@ -42,4 +42,13 @@ class ValidationTest extends TestCase
|
||||
]);
|
||||
$response->assertStatus(200);
|
||||
}
|
||||
|
||||
public function test_validation_errors_shown_in_blade()
|
||||
{
|
||||
// Post without name and description should fail
|
||||
$response = $this->followingRedirects()->post('projects');
|
||||
$response->assertStatus(200);
|
||||
$response->assertSee('The name field is required.');
|
||||
$response->assertSee('The description field is required.');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user