Laravelでバリデーションを日本語にする

Laravelのバリデーションを日本語にする方法を調べたのでメモしておきます。

対応前

app\Http\routes.php

Route::get('/post', 'PostController@getIndex');
Route::post('/post', 'PostController@postIndex');

app\Http\Controllers\PostController.php

<?php

namespace App\Http\Controllers;

use Request;
use Redirect;
use Validator;

class PostController extends Controller
{
    public function getIndex()
    {
        return view('post.index');
    }

    // バリデーションのルール
    public $validateRules = [
        'mail'=>'required|email',
        'password'=>'required|min:8'
    ];

    public function postIndex()
    {
		// postしたデータをすべて取得
		$data = Request::all();

        //バリデーションをインスタンス化
        $val = Validator::make(
			$data,
			$this->validateRules
		);

        //バリデーションNGの場合
        if($val->fails()){
            return redirect('/post/')->withErrors($val)->withInput();
        }

		return 'OK!';
    }
}

resources\views\post\index.blade.php

<form action="/post" method="POST" novalidate>
	<dl>
		<dt>メールアドレス<span class="required">(必須)</span></dt>
		<dd>
			<input name="mail" type="text">
			@if($errors->has('mail'))<p class="error">{{ $errors->first('mail') }}</p> @endif
		</dd>
	</dl>
	<dl>
		<dt>パスワード<span class="required">(必須・8文字以上)</span></dt>
		<dd>
			<input name="password" type="text">
			@if($errors->has('password'))<p class="error">{{ $errors->first('password') }}</p> @endif
		</dd>
	</dl>
	<div>
		<input type="submit" value="送信">
	</div>
	<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>

特に設定せずにバリデーションを実装した場合、エラー文言が英語で表示されます。

to-japanese-validation-in-laravel01

 

対応方法

今回はこちらのメッセージファイルを使ってみます。
resources\lang 内にjaディレクトリを作成して、その中にvalidation.phpを作成します。

resources\lang\ja\validation.php

<?php  // resources/lang/ja/validation.php
return [
    /*
    |--------------------------------------------------------------------------
    | Validation Language Lines
    |--------------------------------------------------------------------------
    |
    | The following language lines contain the default error messages used by
    | the validator class. Some of these rules have multiple versions such
    | as the size rules. Feel free to tweak each of these messages here.
    |
    */
    'accepted'             => ':attributeを承認してください。',
    'active_url'           => ':attributeは正しいURLではありません。',
    'after'                => ':attributeは:date以降の日付にしてください。',
    'alpha'                => ':attributeは英字のみにしてください。',
    'alpha_dash'           => ':attributeは英数字とハイフンのみにしてください。',
    'alpha_num'            => ':attributeは英数字のみにしてください。',
    'array'                => ':attributeは配列にしてください。',
    'before'               => ':attributeは:date以前の日付にしてください。',
    'between'              => [
        'numeric' => ':attributeは:min〜:maxまでにしてください。',
        'file'    => ':attributeは:min〜:max KBまでのファイルにしてください。',
        'string'  => ':attributeは:min〜:max文字にしてください。',
        'array'   => ':attributeは:min〜:max個までにしてください。',
    ],
    'boolean'              => ':attributeはtrueかfalseにしてください。',
    'confirmed'            => ':attributeは確認用項目と一致していません。',
    'date'                 => ':attributeは正しい日付ではありません。',
    'date_format'          => ':attributeは":format"書式と一致していません。',
    'different'            => ':attributeは:otherと違うものにしてください。',
    'digits'               => ':attributeは:digits桁にしてください',
    'digits_between'       => ':attributeは:min〜:max桁にしてください。',
    'email'                => ':attributeを正しいメールアドレスにしてください。',
    'filled'               => ':attributeは必須です。',
    'exists'               => '選択された:attributeは正しくありません。',
    'image'                => ':attributeは画像にしてください。',
    'in'                   => '選択された:attributeは正しくありません。',
    'integer'              => ':attributeは整数にしてください。',
    'ip'                   => ':attributeを正しいIPアドレスにしてください。',
    'max'                  => [
        'numeric' => ':attributeは:max以下にしてください。',
        'file'    => ':attributeは:max KB以下のファイルにしてください。.',
        'string'  => ':attributeは:max文字以下にしてください。',
        'array'   => ':attributeは:max個以下にしてください。',
    ],
    'mimes'                => ':attributeは:valuesタイプのファイルにしてください。',
    'min'                  => [
        'numeric' => ':attributeは:min以上にしてください。',
        'file'    => ':attributeは:min KB以上のファイルにしてください。.',
        'string'  => ':attributeは:min文字以上にしてください。',
        'array'   => ':attributeは:min個以上にしてください。',
    ],
    'not_in'               => '選択された:attributeは正しくありません。',
    'numeric'              => ':attributeは数字にしてください。',
    'regex'                => ':attributeの書式が正しくありません。',
    'required'             => ':attributeは必須です。',
    'required_if'          => ':otherが:valueの時、: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 KBにしてください。.',
        'string'  => ':attribute:size文字にしてください。',
        'array'   => ':attributeは:size個にしてください。',
    ],
    'string'               => ':attributeは文字列にしてください。',
    'timezone'             => ':attributeは正しいタイムゾーンをしていしてください。',
    'unique'               => ':attributeは既に存在します。',
    'url'                  => ':attributeを正しい書式にしてください。',
    /*
    |--------------------------------------------------------------------------
    | Custom Validation Language Lines
    |--------------------------------------------------------------------------
    |
    | Here you may specify custom validation messages for attributes using the
    | convention "attribute.rule" to name the lines. This makes it quick to
    | specify a specific custom language line for a given attribute rule.
    |
    */
    'custom' => [
        'attribute-name' => [
            'rule-name' => 'custom-message',
        ],
    ],
    /*
    |--------------------------------------------------------------------------
    | Custom Validation Attributes
    |--------------------------------------------------------------------------
    |
    | The following language lines are used to swap attribute place-holders
    | with something more reader friendly such as E-Mail Address instead
    | of "email". This simply helps us make messages a little cleaner.
    |
    */
    'attributes' => [],
];

config\app.php

config\app.phpの下記を変更します。

	// 変更前
    'locale' => 'en',

	// 変更後
    'locale' => 'ja',

エラー文言が日本語で表示されました。

to-japanese-validation-in-laravel02

resources\lang\en\validation.phpにデフォルトの英語のエラーメッセージがあるので、こちらをコピーしてメッセージ部分を書き換える方法でもできます。
 

【参考サイト】

 

このエントリーをはてなブックマークに追加

関連記事

コメントを残す

メールアドレスが公開されることはありません。
* が付いている欄は必須項目です

CAPTCHA


コメントが承認されるまで時間がかかります。

2024年5月
 1234
567891011
12131415161718
19202122232425
262728293031