<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                ## lumen 表單驗證 #### 創建控制器 接下來,讓我們來看下操作這些路由的控制器。我們先讓`store`方法空著: ~~~php <?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Controllers\Controller; class PostController extends Controller { /** * 顯示創建博客文章的表單。 * * @return Response */ public function create() { return view('post.create'); } /** * 保存一個新的博客文章。 * * @param Request $request * @return Response */ public function store(Request $request) { // 驗證以及保存博客發表文章... } } ~~~ #### 在控制器中編寫驗證邏輯 我們會使用了`ValidatesRequests``trait`類。這個 `trait` 在你所有的控制器里提供了方便的`validate`驗證方法。 `validate`方法會接收 HTTP 傳入的請求以及驗證的規則。如果驗證通過,你的代碼就可以正常的運行。若驗證失敗,則會拋出異常錯誤消息并自動將其返回給用戶。在一般的 HTTP 請求下,都會生成一個重定向響應,對于 AJAX 請求則會發送 JSON 響應。 讓我們接著回到`store`方法來深入理解`validate`方法: ~~~php /** * 保存一篇新的博客文章。 * * @param Request $request * @return Response */ public function store(Request $request) { $this->validate($request, [ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // 博客文章成功發表,將其保存到數據庫... } ~~~ * 如果驗證失敗,將會自動生成一個對應的響應 * 如果驗證通過,那我們的控制器將會繼續正常運行 * 如果是跨域請求,也是直接返回錯誤 #### 顯示驗證錯誤 * Laravel 會自動檢查 session 內的錯誤數據,如果錯誤存在的話,它會自動將這些錯誤消息綁定到視圖上。 * 錯誤信息`$errors`變量在每次請求的所有視圖中都可以被使用 * `$errors`變量是`Illuminate\Support\MessageBag`的實例。 * 如果是跨域等請求,可以也讀取 錯誤信息`$errors` 所以,在我們的例子中,當驗證失敗時,用戶將被重定向到我們的控制器`create`方法,讓我們在視圖中顯示錯誤的消息: ~~~php <!-- /resources/views/post/create.blade.php --> <h1>創建文章</h1> @if (count($errors) > 0) <div class="alert alert-danger"> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif <!-- 創建文章的表單 --> ~~~ ### 手動創建驗證程序 如果你不想要使用`ValidatesRequests`trait 的`validate`方法,你可以手動創建一個 validator 實例并通過`Validator::make`方法在[facade](https://learnku.com/docs/laravel/5.1/facades)生成一個新的 validator 實例: ~~~php <?php namespace App\Http\Controllers; use Validator; use Illuminate\Http\Request; use App\Http\Controllers\Controller; class PostController extends Controller { /** * 保存一篇新的博客文章。 * * @param Request $request * @return Response */ public function store(Request $request) { $validator = Validator::make($request->all(), [ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); if ($validator->fails()) { return redirect('post/create') ->withErrors($validator) ->withInput(); } // 保存文章... } } ~~~ 第一個傳給`make`方法的參數是驗證數據。第二個參數則是數據的驗證規則。 如果請求沒有通過驗證,則可以使用`withErrors`方法把錯誤消息閃存到 session。在進行重定向之后,`$errors`變量可以在視圖中自動共用,讓你可以輕松地顯示這些消息并返回給用戶。`withErrors`方法接收 validator、`MessageBag`,或 PHP`array`。 #### 命名錯誤清單 假如在一個頁面中有許多表單,你可能希望為`MessageBag`的錯誤命名,這可以讓你獲取特定表單的所有錯誤消息。只需在`withErrors`的第二個參數設置名稱即可: ~~~php return redirect('register') ->withErrors($validator, 'login'); ~~~ 然后你就可以從一個`$errors`變量中,獲取已命名的`MessageBag`實例: ~~~php {{ $errors->login->first('email') }} ~~~ #### 驗證后的掛勾 在驗證完成之后,validator 可以讓你附加返回消息。你可以更簡單的做進一步的驗證以及增加更多的錯誤消息到消息集合上。在 validator 實例使用`after`方法如下所示: ~~~php $validator = Validator::make(...); $validator->after(function($validator) { if ($this->somethingElseIsInvalid()) { $validator->errors()->add('field', 'Something is wrong with this field!'); } }); if ($validator->fails()) { // } ~~~ ## 處理錯誤消息 調用一個`Validator`實例的`errors`方法,會得到一個`Illuminate\Support\MessageBag`的實例,里面有許多可讓你操作錯誤消息的便利方法。 #### 查看特定字段的第一個錯誤消息 如果要查看特定字段的第一個錯誤消息,可以使用`first`方法: ~~~php $messages = $validator->errors(); echo $messages->first('email'); ~~~ #### 查看特定字段的所有錯誤消息 如果你想通過指定字段來簡單的獲取所有消息中的一個數組,則可以使用`get`方法: ~~~php foreach ($messages->get('email') as $message) { // } ~~~ #### 查看所有字段的所有錯誤消息 如果你想要得到所有字段的消息數組,則可以使用`all`方法: ~~~php foreach ($messages->all() as $message) { // } ~~~ #### 判斷特定字段是否含有錯誤消息 ~~~php if ($messages->has('email')) { // } ~~~ #### 獲取格式化后的錯誤消息 ~~~php echo $messages->first('email', '<p>:message</p>'); ~~~ #### 獲取所有格式化后的錯誤消息 ~~~php foreach ($messages->all('<li>:message</li>') as $message) { // } ~~~ ### 自定義錯誤消息 如果有需要的話,你也可以自定義錯誤的驗證消息來取代默認的驗證消息。有幾種方法可以來自定義指定的消息。首先,你需要先通過傳遞三個參數到`Validator::make`方法來自定義驗證消息: ~~~php $messages = [ 'required' => 'The :attribute field is required.', ]; $validator = Validator::make($input, $rules, $messages); ~~~ 在這個例子中,`:attribute`占位符會被通過驗證的字段實際名稱所取代。除此之外,你還可以使用其它默認字段的驗證消息。例如: ~~~php $messages = [ 'same' => 'The :attribute and :other must match.', 'size' => 'The :attribute must be exactly :size.', 'between' => 'The :attribute must be between :min - :max.', 'in' => 'The :attribute must be one of the following types: :values', ]; ~~~ #### 指定自定義消息到特定的屬性 有時候你可能想要對特定的字段來自定義錯誤消息。只需在屬性名稱后加上「.」符號和指定驗證的規則即可: ~~~php $messages = [ 'email.required' => 'We need to know your e-mail address!', ]; ~~~ #### 在語言包中自定義指定消息 在許多情況下,你可能希望在語言包中被指定的特定屬性自定義消息不被直接傳到`Validator`上。因此你可以把消息加入到`resources/lang/xx/validation.php`語言包中的`custom`數組。 ~~~php 'custom' => [ 'email' => [ 'required' => 'We need to know your e-mail address!', ], ], ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看