mirror of
https://github.com/10h30/Test-Laravel-Validation.git
synced 2026-06-05 15:07:56 +09:00
Task 4 - showing specific validation error
This commit is contained in:
@@ -56,3 +56,11 @@ Test method `test_validation_errors_shown_in_blade()`.
|
||||
|
||||
---
|
||||
|
||||
## Task 4. Showing a Specific Validation Error.
|
||||
|
||||
In `resources/views/products/create.blade.php` file, show the validation error for `"name" => "required"`, using a specific Blade directive to show one specific error.
|
||||
|
||||
Test method `test_validation_specific_error_shown_in_blade()`.
|
||||
|
||||
---
|
||||
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Product;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
|
||||
class ProductController extends Controller
|
||||
{
|
||||
public function create()
|
||||
{
|
||||
return view('products.create');
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
{
|
||||
$validator = Validator::make($request->all(), [
|
||||
'name' => 'required',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return redirect('products/create')
|
||||
->withErrors($validator);
|
||||
}
|
||||
|
||||
Product::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 Product 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 CreateProductsTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('products', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('name');
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('products');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<form method="POST" action="{{ route('products.store') }}">
|
||||
@csrf
|
||||
Name:
|
||||
<br />
|
||||
<input type="text" name="name" />
|
||||
<br />
|
||||
{{-- TASK: show the validation error for the specific "name" field --}}
|
||||
{{-- using one Blade directive: pseudo-code below --}}
|
||||
{{-- @directive --}}
|
||||
{{-- {{ $message }} --}}
|
||||
{{-- @endDirective --}}
|
||||
<br /><br />
|
||||
<button type="submit">Save</button>
|
||||
</form>
|
||||
@@ -16,6 +16,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::resource('products', \App\Http\Controllers\ProductController::class);
|
||||
|
||||
Route::get('/', function () {
|
||||
return view('welcome');
|
||||
|
||||
@@ -51,4 +51,12 @@ class ValidationTest extends TestCase
|
||||
$response->assertSee('The name field is required.');
|
||||
$response->assertSee('The description field is required.');
|
||||
}
|
||||
|
||||
public function test_validation_specific_error_shown_in_blade()
|
||||
{
|
||||
// Post without name should fail
|
||||
$response = $this->followingRedirects()->post('products');
|
||||
$response->assertStatus(200);
|
||||
$response->assertSee('The name field is required.');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user