Skip to content

Commit

Permalink
バリデーション実装
Browse files Browse the repository at this point in the history
  • Loading branch information
ryotamedx committed Sep 19, 2021
1 parent 801ecc5 commit d82bd01
Show file tree
Hide file tree
Showing 7 changed files with 237 additions and 15 deletions.
5 changes: 3 additions & 2 deletions app/Http/Controllers/ItemController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Http\Request;
use App\Models\Item;
use App\Http\Requests\ItemRequest;

class ItemController extends Controller
{
Expand All @@ -24,7 +25,7 @@ public function create()
return view('items.create');
}

public function store(Request $request)
public function store(ItemRequest $request)
{
// インスタンス化
$item = new Item;
Expand All @@ -49,7 +50,7 @@ public function edit($id)
return view('items.edit', ['item' => $item]);
}

public function update(Request $request, $id)
public function update(ItemRequest $request, $id)
{
// 元のデータを取得
$item = Item::find($id);
Expand Down
35 changes: 35 additions & 0 deletions app/Http/Requests/ItemRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class ItemRequest 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:50',
'description' => 'required|string|max:2000',
'price' => 'required|integer|min:1',
'seller' => 'required|string|max:50',
'email' => 'required|email:rfc,dns',
'image_url' => 'required|active_url'
];
}
}
2 changes: 1 addition & 1 deletion config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
|
*/

'locale' => 'en',
'locale' => 'ja',

/*
|--------------------------------------------------------------------------
Expand Down
4 changes: 4 additions & 0 deletions public/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@

label {
display: block;
}

.error {
color: red;
}
158 changes: 158 additions & 0 deletions resources/lang/ja/validation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
<?php

return [

/*
|--------------------------------------------------------------------------
| バリデーション言語行
|--------------------------------------------------------------------------
|
| 以下の言語行はバリデタークラスにより使用されるデフォルトのエラー
| メッセージです。サイズルールのようにいくつかのバリデーションを
| 持っているものもあります。メッセージはご自由に調整してください。
|
*/

'accepted' => ':attributeを承認してください。',
'active_url' => ':attributeが有効なURLではありません。',
'after' => ':attributeには、:dateより後の日付を指定してください。',
'after_or_equal' => ':attributeには、:date以降の日付を指定してください。',
'alpha' => ':attributeはアルファベットのみがご利用できます。',
'alpha_dash' => ':attributeはアルファベットとダッシュ(-)及び下線(_)がご利用できます。',
'alpha_num' => ':attributeはアルファベット数字がご利用できます。',
'array' => ':attributeは配列でなくてはなりません。',
'before' => ':attributeには、:dateより前の日付をご利用ください。',
'before_or_equal' => ':attributeには、:date以前の日付をご利用ください。',
'between' => [
'numeric' => ':attributeは、:minから:maxの間で指定してください。',
'file' => ':attributeは、:min kBから、:max kBの間で指定してください。',
'string' => ':attributeは、:min文字から、:max文字の間で指定してください。',
'array' => ':attributeは、:min個から:max個の間で指定してください。',
],
'boolean' => ':attributeは、trueかfalseを指定してください。',
'confirmed' => ':attributeと、確認フィールドとが、一致していません。',
'date' => ':attributeには有効な日付を指定してください。',
'date_equals' => ':attributeには、:dateと同じ日付けを指定してください。',
'date_format' => ':attributeは:format形式で指定してください。',
'different' => ':attributeと:otherには、異なった内容を指定してください。',
'digits' => ':attributeは:digits桁で指定してください。',
'digits_between' => ':attributeは:min桁から:max桁の間で指定してください。',
'dimensions' => ':attributeの図形サイズが正しくありません。',
'distinct' => ':attributeには異なった値を指定してください。',
'email' => ':attributeには、有効なメールアドレスを指定してください。',
'ends_with' => ':attributeには、:valuesのどれかで終わる値を指定してください。',
'exists' => '選択された:attributeは正しくありません。',
'file' => ':attributeにはファイルを指定してください。',
'filled' => ':attributeに値を指定してください。',
'gt' => [
'numeric' => ':attributeには、:valueより大きな値を指定してください。',
'file' => ':attributeには、:value kBより大きなファイルを指定してください。',
'string' => ':attributeは、:value文字より長く指定してください。',
'array' => ':attributeには、:value個より多くのアイテムを指定してください。',
],
'gte' => [
'numeric' => ':attributeには、:value以上の値を指定してください。',
'file' => ':attributeには、:value kB以上のファイルを指定してください。',
'string' => ':attributeは、:value文字以上で指定してください。',
'array' => ':attributeには、:value個以上のアイテムを指定してください。',
],
'image' => ':attributeには画像ファイルを指定してください。',
'in' => '選択された:attributeは正しくありません。',
'in_array' => ':attributeには:otherの値を指定してください。',
'integer' => ':attributeは整数で指定してください。',
'ip' => ':attributeには、有効なIPアドレスを指定してください。',
'ipv4' => ':attributeには、有効なIPv4アドレスを指定してください。',
'ipv6' => ':attributeには、有効なIPv6アドレスを指定してください。',
'json' => ':attributeには、有効なJSON文字列を指定してください。',
'lt' => [
'numeric' => ':attributeには、:valueより小さな値を指定してください。',
'file' => ':attributeには、:value kBより小さなファイルを指定してください。',
'string' => ':attributeは、:value文字より短く指定してください。',
'array' => ':attributeには、:value個より少ないアイテムを指定してください。',
],
'lte' => [
'numeric' => ':attributeには、:value以下の値を指定してください。',
'file' => ':attributeには、:value kB以下のファイルを指定してください。',
'string' => ':attributeは、:value文字以下で指定してください。',
'array' => ':attributeには、:value個以下のアイテムを指定してください。',
],
'max' => [
'numeric' => ':attributeには、:max以下の数字を指定してください。',
'file' => ':attributeには、:max kB以下のファイルを指定してください。',
'string' => ':attributeは、:max文字以下で指定してください。',
'array' => ':attributeは:max個以下指定してください。',
],
'mimes' => ':attributeには:valuesタイプのファイルを指定してください。',
'mimetypes' => ':attributeには:valuesタイプのファイルを指定してください。',
'min' => [
'numeric' => ':attributeには、:min以上の数字を指定してください。',
'file' => ':attributeには、:min kB以上のファイルを指定してください。',
'string' => ':attributeは、:min文字以上で指定してください。',
'array' => ':attributeは:min個以上指定してください。',
],
'not_in' => '選択された:attributeは正しくありません。',
'not_regex' => ':attributeの形式が正しくありません。',
'numeric' => ':attributeには、数字を指定してください。',
'password' => '正しいパスワードを指定してください。',
'present' => ':attributeが存在していません。',
'regex' => ':attributeに正しい形式を指定してください。',
'required' => ':attributeは必ず指定してください。',
'required_if' => ':otherが:valueの場合、:attributeも指定してください。',
'required_unless' => ':otherが:valuesでない場合、:attributeを指定してください。',
'required_with' => ':valuesを指定する場合は、:attributeも指定してください。',
'required_with_all' => ':valuesを指定する場合は、:attributeも指定してください。',
'required_without' => ':valuesを指定しない場合は、:attributeを指定してください。',
'required_without_all' => ':valuesのどれも指定しない場合は、:attributeを指定してください。',
'same' => ':attributeと:otherには同じ値を指定してください。',
'size' => [
'numeric' => ':attributeは:sizeを指定してください。',
'file' => ':attributeのファイルは、:sizeキロバイトでなくてはなりません。',
'string' => ':attributeは:size文字で指定してください。',
'array' => ':attributeは:size個指定してください。',
],
'starts_with' => ':attributeには、:valuesのどれかで始まる値を指定してください。',
'string' => ':attributeは文字列を指定してください。',
'timezone' => ':attributeには、有効なゾーンを指定してください。',
'unique' => ':attributeの値は既に存在しています。',
'uploaded' => ':attributeのアップロードに失敗しました。',
'url' => ':attributeに正しい形式を指定してください。',
'uuid' => ':attributeに有効なUUIDを指定してください。',

/*
|--------------------------------------------------------------------------
| Custom バリデーション言語行
|--------------------------------------------------------------------------
|
| "属性.ルール"の規約でキーを指定することでカスタムバリデーション
| メッセージを定義できます。指定した属性ルールに対する特定の
| カスタム言語行を手早く指定できます。
|
*/

'custom' => [
'属性名' => [
'ルール名' => 'カスタムメッセージ',
],
],

/*
|--------------------------------------------------------------------------
| カスタムバリデーション属性名
|--------------------------------------------------------------------------
|
| 以下の言語行は、例えば"email"の代わりに「メールアドレス」のように、
| 読み手にフレンドリーな表現でプレースホルダーを置き換えるために指定する
| 言語行です。これはメッセージをよりきれいに表示するために役に立ちます。
|
*/

'attributes' => [
'name' => '商品名',
'description' => '商品詳細',
'price' => '価格',
'seller' => '出品者',
'email' => '電子メール',
'image_url' => '商品画像URL'
],

];
24 changes: 18 additions & 6 deletions resources/views/items/create.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,44 @@
<link rel="stylesheet" href="{{ asset('css/style.css') }}">
</head>
<body>
@if ($errors->any())
<div class="error">
<p>
<b>{{ count($errors) }}件のエラーがあります。</b>
</p>
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<h1>商品登録フォーム</h1>
<form action="/items" method="post">
@csrf
<p>
<label for="name">商品名</label>
<input type="text" name="name" id="">
<input type="text" name="name" id="" value="{{ old('name') }}">
</p>
<p>
<label for="description">商品詳細</label>
<input type="text" name="description" id="">
<input type="text" name="description" id="" value="{{ old('description') }}">
</p>
<p>
<label for="price">価格</label>
<input type="text" name="price" id="">
<input type="text" name="price" id="" value="{{ old('price') }}">
</p>
<p>
<label for="seller">出品者</label>
<input type="text" name="seller" id="">
<input type="text" name="seller" id="" value="{{ old('seller') }}">
</p>
<p>
<label for="email">電子メール</label>
<input type="text" name="email" id="">
<input type="text" name="email" id="" value="{{ old('email') }}">
</p>
<p>
<label for="image_url">商品画像URL</label>
<input type="text" name="image_url" id="">
<input type="text" name="image_url" id="" value="{{ old('image_url') }}">
</p>

<input type="submit" value="登録">
Expand Down
24 changes: 18 additions & 6 deletions resources/views/items/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,45 @@
<link rel="stylesheet" href="{{ asset('css/style.css') }}">
</head>
<body>
@if ($errors->any())
<div class="error">
<p>
<b>{{ count($errors) }}件のエラーがあります。</b>
</p>
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<h1>商品更新フォーム</h1>
<form action="/items/{{ $item->id }}" method="post">
@csrf
@method('PATCH')
<p>
<label for="name">商品名</label>
<input type="text" name="name" id="" value="{{ $item->name }}">
<input type="text" name="name" id="" value="{{ old('name', $item->name) }}">
</p>
<p>
<label for="description">商品詳細</label>
<input type="text" name="description" id="" value="{{ $item->description }}">
<input type="text" name="description" id="" value="{{ old('description', $item->description) }}">
</p>
<p>
<label for="price">価格</label>
<input type="text" name="price" id="" value="{{ $item->price }}">
<input type="text" name="price" id="" value="{{ old('price', $item->price) }}">
</p>
<p>
<label for="seller">出品者</label>
<input type="text" name="seller" id="" value="{{ $item->seller }}">
<input type="text" name="seller" id="" value="{{ old('seller', $item->seller) }}">
</p>
<p>
<label for="email">電子メール</label>
<input type="text" name="email" id="" value="{{ $item->email }}">
<input type="text" name="email" id="" value="{{ old('email', $item->email) }}">
</p>
<p>
<label for="image_url">商品画像URL</label>
<input type="text" name="image_url" id="" value="{{ $item->image_url }}">
<input type="text" name="image_url" id="" value="{{ old('image_url', $item->image_url) }}">
</p>

<input type="submit" value="更新">
Expand Down

0 comments on commit d82bd01

Please sign in to comment.