1. 程式人生 > >laravel表單自定義驗證提示資訊

laravel表單自定義驗證提示資訊

  laravel的驗證器中,預設的提示資訊是英文,但在實際開發中,經常會使用中文的提示資訊,尤其是在驗證表單輸入時。
  由於官方文件寫得很是簡略,所以當時在使用驗證器自定義提示資訊時,還是很花了一番功夫,網上也查了一些資料,但都是神龍見首不見尾,因此在此記錄下,讓初學者少走彎路。
  自定義驗證器的內容如下,相信只要有點PHP功底的,都能看懂,看不懂的自己去補PHP基礎。

public function store(Request $request)
{
    //定義驗證規則,是一個數組
    $rules = [
        'title' => 'required|max:64'
, 'content' => 'required|max:500', 'back_color' => 'required|max:10', 'user_name' => 'required|max:10', ]; //定義提示資訊 $messages = [ 'title.required' => '留言標題不能為空', 'content.required' => '留言內容不能為空', 'back_color.required' => '留言背景顏色不能為空'
, 'user_name.required' => '留言者姓名不能為空', ]; //建立驗證器 $validator = Validator::make($request->all(), $rules, $messages); if ($validator->fails()) { return redirect('/index') ->withErrors($validator) ->withInput(); } $posts
= Posts::create($request->all()); return redirect('/index'); }

  雖然定義好了驗證訊息,但是怎麼在模板中使用自定義的驗證訊息,卡了我很久,看到官方的common.errors中輸出驗證訊息的原始碼,修改如下以顯示索引資訊:

@if (count($errors) > 0)
    <!-- Form Error List -->
    <div class="alert alert-danger">
        <strong>Whoops! Something went wrong!</strong>

        <br><br>

        <ul>
            @foreach ($errors->all() as $k => $error)
                <li>{{ $k }} => {{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

  發現他是一個索引陣列,於是想在表單中直接引用,但是直接使用索引輸出會出現問題,使用索引輸出的程式碼如下:

$errors->all()[0];//輸出第一個提示資訊

  使用這種方法,直接將提示資訊寫死了,假設表單中有5條資訊,那麼就應該有5個驗證訊息,使用者有多少個資訊沒有填,就會產生多少條提示資訊,如果直接使用索引陣列下標的方式輸出,程式無法判斷使用錯誤提示資訊中的哪一個索引。

  正確的引用提示訊息的程式碼如下:

<form action="{{ url('store') }}" method="POST" class="form-horizontal">
 {{ csrf_field() }}

    <fieldset>
        <div class="fhint">
            <label id="fhint">新增文章</label>
        </div>

        <div class="ftitle">
            <label></label>
            <input>
            @if ($errors->has('title'))
                <label></label>
                <p style="display: inline; background-color: red">{{ $errors->first('title') }}</p >
            @endif
        </div>
        <div class="fcontent">
            <label </label>
            <textarea></textarea>
            @if ($errors->has('content'))
                <label></label>
                <p style="display: inline; background-color: red">{{ $errors->first('content') }}</p >
            @endif
        </div>
        <div class="fbackcolor">
            <label></label>
            <select name="back_color" id="fbackcolor" onchange="checkValue(event);">
                <option selected="true" disabled="disabled" value>請選擇黃色或紫色</option>
                <option value="yellow">黃色</option>
                <option value="#ff00ff">紫色</option>
            </select>
                @if ($errors->has('back_color'))
                    <label></label>
                    <p style="display: inline; background-color: red">{{ $errors->first('back_color') }}</p >
                @endif
        </div>

        <div class="fname">
            <label for="fname">名字</label>
            <input type="text" id="fname" name="user_name" onkeyup="checkValue(event);">
            @if ($errors->has('user_name'))
                <label></label>
                <p style="display: inline; background-color: red">{{ $errors->first('user_name') }}</p >
            @endif
        </div>
        <div class="fsubmit">
            <label></label>
            <input type="submit" value="提交" id="submit">
        </div>
    </fieldset>
</form>