バリデーションはコントローラに書かずに、フォームリクエストに書く。
(コントローラとルートの間?)
今回は計4つ
ターミナルで
> php artisan make:request Item\StoreRequest
できあがったStoreRequestを下記のように修正
Item/StoreRequest.php
<?php
namespace App\Http\Requests\Item;
use Illuminate\Foundation\Http\FormRequest;
class StoreRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'category_id' => ['required', 'numeric', 'exists:categories,id'], //numeric = 数値
'name' => ['required', 'string', 'max:100', 'unique:items'], //同じ名前のカテゴリーがあった場合は作成出来ないようにする。
];
}
}
コントローラのstoreメソッドを書き換える
ItemController.php
use App\Http\Requests\Item\Request;
// ↓このように
use App\Http\Requests\Item\StoreRequest;
public function store(Request $request)
// ↓このように
public function store(StoreRequest $request)
items/create.blade.php
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
後の3箇所も同様に。
viewとcontrollerは省略
> php artisan make:request Category\StoreRequest
/StoreRequest.php
<?php
namespace App\Http\Requests\Category;
use Illuminate\Foundation\Http\FormRequest;
class StoreRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => ['required', 'string', 'max:20', 'unique:categories'],
];
}
}
> php artisan make:request Item\StoreRequest
Item/UpdateRequest.php
<?php
namespace App\Http\Requests\Item;
use App\Item;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
class UpdateRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
$item = $this->route()->parameter('item');
assert($item instanceof Item);
return [
'category_id' => ['required', 'numeric', 'exists:categories,id'],
'name' => ['required', 'string', 'max:100', Rule::unique('items')->ignore($item->id)],
];
}
}
> php artisan make:request Category\UpdateRequest
Category/UpdateRequest.php
<?php
namespace App\Http\Requests\Category;
use App\Category;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
class UpdateRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
$category = $this->route()->parameter('category');
assert($category instanceof Category);
return [
'name' => ['required', 'string', 'max:20', Rule::unique('categories')->ignore($category->id)],
];
}
}
完成!
Laravelの公式サイト:その他バリデーションについて
https://readouble.com/laravel/5.5/ja/validation.html