バリデーションをFormRequestで行う
This commit is contained in:
parent
74c8a1b6cb
commit
a998d7132f
app/Http
@ -3,6 +3,7 @@
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\AdminInfoStoreRequest;
|
||||
use App\Information;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
@ -31,21 +32,13 @@ class InfoController extends Controller
|
||||
]);
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
public function store(AdminInfoStoreRequest $request)
|
||||
{
|
||||
$inputs = $request->all();
|
||||
if (!$request->has('pinned')) {
|
||||
$inputs['pinned'] = false;
|
||||
}
|
||||
|
||||
// TODO: #updateと全く同じだし、フォームリクエストにしたほうがよいのでは?
|
||||
Validator::make($inputs, [
|
||||
'category' => ['required', Rule::in(array_keys(Information::CATEGORIES))],
|
||||
'pinned' => 'nullable|boolean',
|
||||
'title' => 'required|string|max:255',
|
||||
'content' => 'required|string|max:10000'
|
||||
])->validate();
|
||||
|
||||
$info = Information::create($inputs);
|
||||
|
||||
return redirect()->route('admin.info.edit', ['info' => $info])->with('status', 'お知らせを更新しました。');
|
||||
@ -61,20 +54,13 @@ class InfoController extends Controller
|
||||
]);
|
||||
}
|
||||
|
||||
public function update(Request $request, Information $info)
|
||||
public function update(AdminInfoStoreRequest $request, Information $info)
|
||||
{
|
||||
$inputs = $request->all();
|
||||
if (!$request->has('pinned')) {
|
||||
$inputs['pinned'] = false;
|
||||
}
|
||||
|
||||
Validator::make($inputs, [
|
||||
'category' => ['required', Rule::in(array_keys(Information::CATEGORIES))],
|
||||
'pinned' => 'nullable|boolean',
|
||||
'title' => 'required|string|max:255',
|
||||
'content' => 'required|string|max:10000'
|
||||
])->validate();
|
||||
|
||||
$info->fill($inputs)->save();
|
||||
|
||||
return redirect()->route('admin.info.edit', ['info' => $info])->with('status', 'お知らせを更新しました。');
|
||||
|
35
app/Http/Requests/AdminInfoStoreRequest.php
Normal file
35
app/Http/Requests/AdminInfoStoreRequest.php
Normal file
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use App\Information;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class AdminInfoStoreRequest 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' => ['required', Rule::in(array_keys(Information::CATEGORIES))],
|
||||
'pinned' => 'nullable|boolean',
|
||||
'title' => 'required|string|max:255',
|
||||
'content' => 'required|string|max:10000'
|
||||
];
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user